php - what means new static? -


this question has answer here:

i saw in frameworks line of code:

return new static($view, $data); 

how understand new static?

when write new self() inside class's member function, instance of class. that's magic of self keyword.

so:

class foo {    public static function baz() {       return new self();    } }  $x = foo::baz();  // $x `foo` 

you foo if static qualifier used derived class:

class bar extends foo { }  $z = bar::baz();  // $z `foo` 

if want enable polymorphism (in sense), , have php take notice of qualifier used, can swap self keyword static keyword:

class foo {    public static function baz() {       return new static();    } }  class bar extends foo { }  $wow = bar::baz();  // $wow `bar`, though `baz()` in base `foo` 

this made possible php feature known late static binding; don't confuse other, more conventional uses of keyword static.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -