<span>class</span><span> A { </span><span>public</span><span>static</span><span>function</span><span> get_self() { </span><span>return</span><span>new</span><span> self(); } </span><span>public</span><span>static</span><span>function</span><span> get_static() { </span><span>return</span><span>new</span><span>static</span><span>(); } } </span><span>class</span> B <span>extends</span><span> A {} </span><span>echo</span><span>get_class</span>(B::get_self()); <span>//</span><span> A</span><span>echo</span><span>get_class</span>(B::get_static()); <span>//</span><span> B</span><span>echo</span><span>get_class</span>(A::get_static()); <span>//</span><span> A</span>
self 指的是解析上下文,而不是呼叫上下文。例子裡,self被解析為定義get_self()的A,而不是解析為呼叫self的B。
php5.3中引入了延遲靜態綁定的概念,此特性最明顯的標誌就是新關鍵字static。 static指的是被呼叫的類別。例子裡B::get_static()將生產新的B,而不是實例化一個A
以上就介紹了php new self和new static,包含了方面的內容,希望對PHP教學有興趣的朋友有幫助。