<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教程有兴趣的朋友有所帮助。