對(duì)于大多數(shù) PHPer 來(lái)說(shuō),self 與 static 兩個(gè) PHP 關(guān)鍵詞都不算陌生。我們學(xué)會(huì)通過(guò)self::xxxx
這種方式來(lái)調(diào)用當(dāng)前類(lèi)的靜態(tài)屬性和方法。而 static 呢?想必很多人只知道它是用于定義一個(gè)靜態(tài)方法和類(lèi)屬性關(guān)鍵詞。
這也是我之前的認(rèn)知。
現(xiàn)在我們來(lái)回顧一下這兩個(gè)關(guān)鍵詞的一些常見(jiàn)用法:
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
|
// self 用法 1 :調(diào)用靜態(tài)成員屬性 class Person { protected static $maxAddressCount = 5; // 收獲地址創(chuàng)建最大數(shù)量。 public function test() { echo self:: $maxAddressCount ; } } $person = new Person(); $person ->test(); |
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
33
34
35
36
|
// self 用法 2 :調(diào)用靜態(tài)方法 class Person { protected static $maxAddressCount = 5; // 收獲地址創(chuàng)建最大數(shù)量。 protected static function getMaxAddressCount() { return self:: $maxAddressCount ; } public function test() { echo self::getMaxAddressCount(); } } $person = new Person(); $person ->test(); |
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
// self 用法 3 :創(chuàng)建一個(gè)當(dāng)前對(duì)象 // 單例示例 class Person { private static $instance = null; private function __construct() {} final public static function getInstance() { if (self:: $instance == null) { self:: $instance = new self; } return self:: $instance ; } public function test() { echo "hello world!" ; } } $person = Person::getInstance(); $person ->test(); |
關(guān)于 static 關(guān)鍵詞的常見(jiàn)用法也在上面 3 個(gè)示例中得到綜合體現(xiàn)
我深信上面的用法,任何一個(gè)入門(mén)的 PHPer 都是非常熟悉的。現(xiàn)在我要講的是以下兩種方式:
new self()
與 new static()
的區(qū)別?
我相信很多人都知道new self()
創(chuàng)建一個(gè)當(dāng)前類(lèi)的對(duì)象,并不知道new static()
也能創(chuàng)建一個(gè)當(dāng)前類(lèi)的對(duì)象。
關(guān)于new static()
這種用法呢,在官方文檔有說(shuō)明。地址:https://www.php.net/manual/zh/language.oop5.late-static-bindings.php
PHP 官方把這種方式稱為:后期靜態(tài)綁定。
官方示例 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
|
class A { public static function who() { echo __CLASS__ ; } public static function test() { self::who(); } } class B extends A { public static function who() { echo __CLASS__ ; } } B::test(); |
因?yàn)?Class B 繼承了 Class A。 A 與 B 都有一個(gè)靜態(tài)方法who()
。此時(shí)通過(guò)B::test()
的時(shí)候,調(diào)用的實(shí)際上是 Class A 的who()
方法。
因?yàn)樽宇?lèi) Class B 的靜態(tài)方法who()
屬于在 Class A 之后的子類(lèi)里面才定義的。而 PHP 的默認(rèn)特性只允許調(diào)用最先定義的。
就這引出了后期靜態(tài)綁定的概念。
官方示例 2:
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
|
class A { public static function who() { echo __CLASS__ ; } public static function test() { static ::who(); // 后期靜態(tài)綁定從這里開(kāi)始 } } class B extends A { public static function who() { echo __CLASS__ ; } } B::test(); |
我們把 Class A 里面的test()
方法體的self
更改為static
之后,static 代表的永遠(yuǎn)是指向調(diào)用類(lèi)。也就是說(shuō)雖然在 Class A 父類(lèi)里面定義的方法與子類(lèi)有同名沖突的情況。但是,當(dāng)子類(lèi)調(diào)用的時(shí)候,那么自動(dòng)切換到子類(lèi)的靜態(tài)同名方法。取決于調(diào)用者。
大家可以通過(guò)運(yùn)行以上兩個(gè)示例進(jìn)行理解。
之所以會(huì)有本篇小節(jié)內(nèi)容。是因?yàn)槲以趯?shí)際運(yùn)行當(dāng)中要繼承單例方法導(dǎo)致了這個(gè)問(wèn)題。所以,才牽扯出這個(gè)特性。
到此這篇關(guān)于PHP Class self 與 static 異同與使用詳解的文章就介紹到這了,更多相關(guān)PHP Class self 與 static 內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://www.php.cn/php-weizijiaocheng-481717.html