首頁 >後端開發 >php教程 >PHP之static

PHP之static

WBOY
WBOY原創
2016-07-29 09:02:461222瀏覽

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

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

以上就介紹了PHP之static,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn