Home >Backend Development >PHP Tutorial >php new self() and new static(), phpnewselfstatic_PHP tutorial
<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><br /></span><span>echo</span> <span>get_class</span>(A::get_static()); <span>//</span><span> A</span>
self refers to the parsing context, not the calling context. In the example, self is resolved to A that defines get_self(), rather than to B that calls self.
The concept of delayed static binding was introduced in php5.3. The most obvious sign of this feature is the new keyword static. static refers to the class being called. In the example B::get_static() will produce a new B instead of instantiating an A