PHP Static鍏抽敭瀛
涓轰簡鍏煎PHP4锛屽鏋滄病鏈夋寚瀹氣滃彲瑙佹р濓紝灞炴у拰鏂规硶榛樿涓簆ublic銆
鐢变簬闈欐佹柟娉曚笉闇瑕侀氳繃瀵硅薄鍗冲彲璋冪敤锛屾墍浠ヤ吉鍙橀噺$this鍦ㄩ潤鎬佹柟娉曚腑涓嶅彲鐢ㄣ
闈欐佸睘鎬т笉鍙互鐢卞璞¢氳繃->鎿嶄綔绗︽潵璁块棶銆
鐢::鏂瑰紡璋冪敤涓涓潪闈欐佹柟娉曚細瀵艰嚧涓涓狤_STRICT绾у埆鐨勯敊璇
灏卞儚鍏跺畠鎵鏈夌殑PHP闈欐佸彉閲忎竴鏍凤紝闈欐佸睘鎬у彧鑳借鍒濆鍖栦负涓涓瓧绗﹀兼垨涓涓父閲忥紝涓嶈兘浣跨敤琛ㄨ揪寮忋 鎵浠ヤ綘鍙互鎶婇潤鎬佸睘鎬у垵濮嬪寲涓烘暣鍨嬫垨鏁扮粍锛屼絾涓嶈兘鎸囧悜鍙︿竴涓彉閲忔垨鍑芥暟杩斿洖鍊硷紝涔熶笉鑳芥寚鍚戜竴涓璞°
PHP5.3.0涔嬪悗锛屾垜浠彲浠ョ敤涓涓彉閲忔潵鍔ㄦ佽皟鐢ㄧ被銆備絾璇ュ彉閲忕殑鍊间笉鑳戒负鍏抽敭瀛梥elf, parent 鎴杝tatic銆
Example #1 闈欐佹垚鍛樹唬鐮佺ず渚
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<code><?php
class Foo
{
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
}
class Bar extends Foo
{
public function fooStatic() {
return parent::$my_static;
}
}
print Foo::$my_static . " ";
$foo = new Foo();
print $foo->staticValue() . " ";
print $foo->my_static . " "; // Undefined "Property" my_static
print $foo::$my_static . " ";
$classname = 'Foo';
print $classname::$my_static . " "; // PHP 5.3.0涔嬪悗鍙互鍔ㄦ佽皟鐢
print Bar::$my_static . " ";
$bar = new Bar();
print $bar->fooStatic() . " ";
?></code>
|
Example #2 闈欐佹柟娉曚唬鐮佺ず渚
1
2
3
4
5
6
7
8
9
10
11
|
<code><?php
class Foo {
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
?></code>
|
浠ヤ笂灏辨槸PHP Static鍏抽敭瀛楃殑璇︾粏鍐呭锛屾洿澶氫俊鎭鍏虫敞OD浜戝叾瀹冪浉鍏虫枃绔狅紒