本文目錄一覽:
- 1、php的後期靜態綁定,怎麼解釋?這幅圖輸出是A,C,C,。謝謝!我想了很久都不太明白。
- 2、PHP後期靜態綁定錯誤問題,怎麼解決
- 3、PHP Static延遲靜態綁定用法分析
- 4、php Late static binding 是什麼?
- 5、PHP繼承調用問題,static關鍵字問題
- 6、如何理解php中的後期靜態綁定
php的後期靜態綁定,怎麼解釋?這幅圖輸出是A,C,C,。謝謝!我想了很久都不太明白。
首先說一下基礎的static是相對於public的,面象對像編程時影響繼承許可權的,這你應該知道。
由圖的繼承關係可知:C徹底包含了B和A。
在看答案結果以前,他細觀察發現,三個類里都有同一個名稱who()方法。
系統會用最後一個優先順序最高,進一步的說,你幾乎沒法通過C去調用A、B內的who(),只能重改方法,比如添加個getBWho(){echo B::who();}
然後通過C::getBWho();來調用B內的who();
下面來看運行結果:
test只在B中出現,所以結果必然是test()中運行的三個結果:
第一個:靜態直接指名到姓的調用A內靜態函數,這沒有懸念,必然是A
第二個:parent::是調用上一級的父類,在此題中為A,A中又直接調用static:who();上面說過了,這個who()優先順序最高的在C裡面,無論在你ABC中哪裡調用,只要是static::who()必然是最後定義的那個,覆蓋效應,如果想調用A里的必需指明A::who()或是通過去除static從作用域限制來實現。所以這個who()就是C中定義的who
第三個:self::who與第二個類似的問題,看樣該走B的,注意覆蓋效應,要想調用B內的who必須得B::who(),因為更高級的C已經重寫了這個方法,如果C中沒有who,肯定就是B,依次類推。所以必然還是調用C中的who;
所以答案為:ACC
PHP後期靜態綁定錯誤問題,怎麼解決
可以用於訪問靜態成員,類常量,還可以用於覆蓋類中的屬性和方法。
self,parent 和 static 這三個特殊的關鍵字是用於在類定義的內部對其屬性或方法進行訪問的。
parent用於調用父類中被覆蓋的屬性或方法(出現在哪裡,就將解析為相應類的父類)。
self用於調用本類中的方法或屬性(出現在哪裡,就將解析為相應的類;注意與$this區別,$this指向當前實例化的對象)。
當一個子類覆蓋其父類中的方法時,PHP 不會調用父類中已被覆蓋的方法。是否調用父類的方法取決於子類。
PHP Static延遲靜態綁定用法分析
本文實例講述了PHP
Static延遲靜態綁定用法。分享給大家供大家參考,具體如下:
PHP5.3以後引入了延遲靜態綁定static,它是為了解決什麼問題呢?php的繼承模型中有一個存在已久的問題,那就是在父類中引用擴展類的最終狀態比較困難。來看一個例子。
class
A
{
public
static
function
echoClass(){
echo
__CLASS__;
}
public
static
function
test(){
self::echoClass();
}
}
class
B
extends
A
{
public
static
function
echoClass()
{
echo
__CLASS__;
}
}
B::test();
//輸出A
在PHP5.3中加入了一個新特性:延遲靜態綁定,就是把本來在定義階段固定下來的表達式或變數,改在執行階段才決定,比如當一個子類繼承了父類的靜態表達式的時候,它的值並不能被改變,有時不希望看到這種情況。
下面的例子解決了上面提出的問題:
class
A
{
public
static
function
echoClass(){
echo
__CLASS__;
}
public
static
function
test()
{
static::echoClass();
}
}
class
B
extends
A
{
public
static
function
echoClass(){
echo
__CLASS__;
}
}
B::test();
//輸出B
第8行的static::echoClass();定義了一個靜態延遲綁定方法,直到B調用test的時候才執行原本定義的時候執行的方法。
更多關於PHP相關內容感興趣的讀者可查看本站專題:《php操作office文檔技巧總結(包括word,excel,access,ppt)》、《php日期與時間用法總結》、《php面向對象程序設計入門教程》、《php字元串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
php Late static binding 是什麼?
PHP延遲靜態綁定 Late Static Binding
推薦閱讀以下內容:
As of PHP 5.3.0, PHP implements a feature called late static bindings which can be used to reference the called class in a context of static inheritance.
More precisely, late static bindings work by storing the class named in the last “non-forwarding call”. In case of static method calls, this is the class explicitly named (usually the one on the left of the :: operator); in case of non static method calls, it is the class of the object. A “forwarding call” is a static one that is introduced by self::, parent::, static::, or, if going up in the class hierarchy, forward_static_call(). The function get_called_class() can be used to retrieve a string with the name of the called class and static:: introduces its scope.
This feature was named “late static bindings” with an internal perspective in mind. “Late binding” comes from the fact thatstatic:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information. It was also called a “static binding” as it can be used for (but is not limited to) static method calls.
PHP繼承調用問題,static關鍵字問題
問題出現在,A類中調用靜態方法的寫法static::who();
通常調用靜態方法使用 ClassName::foo() 或者 self::foo();
自PHP 5.3.0 起,PHP 增加了一個叫做後期靜態綁定的功能,用於在繼承範圍內引用靜態調用的類。
「後期綁定」的意思是說,static:: 不再被解析為定義當前方法所在的類,而是在實際運行時計算的。也可以稱之為「靜態綁定」,因為它可以用於(但不限於)靜態方法的調用。
詳情 自己 搜索一下PHP手冊
如何理解php中的後期靜態綁定
使用的保留關鍵字:
static
定義:
static:: 不再被解析為定義當前方法所在的類,而是在實際運行時計算的。也可以稱之為「靜態綁定」,因為它可以用於(但不限於)靜態方法的調用。
self與static的區別:
self調用的就是本身代碼片段這個類,而static調用的是從堆內存中提取出來,訪問的是當前實例化的那個類(即static作用於當前調用的類)
示例一(在靜態環境下)
?phpclass A { public static function who() { echo __CLASS__;
} public static function test() { static::who(); // 後期靜態綁定從這裡開始
}
}class B extends A { public static function who() { echo __CLASS__;
}
}
B::test();?輸出結果是”B”
原創文章,作者:CIKT,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/145564.html