static是属于类本身的,是实例化对象所共有的
使用形式:
static $val;
static function func(){}
调用形式:
类名::$val;
类名::func();
<code><span><span><?php </span><span><span>class</span><span>sta</span>{</span><span>public</span><span>$temp</span> = <span>123</span>; <span>static</span><span>$type</span> = <span>100</span>; <span>static</span><span><span>function</span><span>stat</span><span>()</span>{</span><span>$me</span> = <span>new</span><span>self</span>; <span>//静态方法中不能使用this关键字</span><span>echo</span><span>'common val:'</span>.<span>$me</span>->temp.<span>""</span>; <span>echo</span><span>'static val:'</span>.sta::<span>$type</span>.<span>"<hr>"</span>; <span>echo</span><span>'static val:'</span>.<span>$me</span>::<span>$type</span>.<span>"<hr>"</span>; <span>//静态方法中亦不可调用普通方法</span> } } <span>echo</span><span>'sta:'</span>.sta::<span>$type</span>.<span>""</span>; <span>echo</span> sta::stat(); <span>$a</span> = <span>new</span> sta(); <span>echo</span><span>'a:'</span>.<span>$a</span>::<span>$type</span>.<span>""</span>; <span>$b</span> = <span>new</span> sta(); <span>$a</span>::<span>$type</span> = <span>200</span>; <span>echo</span><span>'b:'</span>.<span>$b</span>::<span>$type</span>.<span>""</span>;</span></span></code>
以上就介绍了PHP之static,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。