php - what means new static? -
this question has answer here:
- new self vs. new static 2 answers
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
Post a Comment