what means new static? [duplicate]
Asked Answered
I

1

78

I saw in some frameworks this line of code:

return new static($view, $data);

how do you understand the new static?

Ignaz answered 9/4, 2013 at 9:55 Comment(14)
What framework was this?Jewett
for example laravel, but i found that only in the core, not while using their apiIgnaz
Check this page for more info; https://mcmap.net/q/64326/-new-self-vs-new-staticPentateuch
PHP late static bindingArdeb
@Allendar: So it's like decltype(*this) with polymorphism disabled? What horrible keyword usage!Kingwood
@Lightness "It was decided not to introduce a new keyword but rather use static that was already reserved." php.net/manual/en/language.oop5.late-static-bindings.phpTimmons
so shortly said, its just calling the constructor of the current class, as far i understandIgnaz
@Ignaz If by current you mean the class in which static is used or the current subclass of it, then yes.Timmons
@LightnessRacesinOrbit: No, it's exactly like decltype(*this). The polymorphism-disabled version would be new self.Efik
@Efik FYI, decltype is already "polymorphism-disabled". As a static construct it seems to be like static here to me. self would be a version that employs RTTI.Kingwood
@Efik https://mcmap.net/q/266292/-does-decltype-give-me-an-object-39-s-static-type-or-its-runtime-type/560648Kingwood
@LightnessRacesinOrbit: Well, I have never used decltype myself so I was going on your earlier comment. "decltype(*this) with polymorphism disabled" implies that normally "decltype(*this)" is polymorphism-enabled.Efik
@Jon: And I was wrong. :)Kingwood
With all due respect I don't understand how this question is a duplicate of the other one. The other question inquires about new static relative to new self whereas this question inquires about new static independent of any other language constructs. For me difference is apparent in question clarity and comprehension.Disbelief
K
130

When you write new self() inside a class's member function, you get an instance of that class. That's the magic of the self keyword.

So:

class Foo
{
   public static function baz() {
      return new self();
   }
}

$x = Foo::baz();  // $x is now a `Foo`

You get a Foo even if the static qualifier you used was for a derived class:

class Bar extends Foo
{
}

$z = Bar::baz();  // $z is now a `Foo`

If you want to enable polymorphism (in a sense), and have PHP take notice of the qualifier you used, you can swap the self keyword for the static keyword:

class Foo
{
   public static function baz() {
      return new static();
   }
}

class Bar extends Foo
{
}

$wow = Bar::baz();  // $wow is now a `Bar`, even though `baz()` is in base `Foo`

This is made possible by the PHP feature known as late static binding; don't confuse it for other, more conventional uses of the keyword static.

Kingwood answered 9/4, 2013 at 10:5 Comment(4)
hmm..shortly: new static() - returns the object of current class, regardless of which classes is extended, and new self() - returns the object from the class in which that method was declared or extended(last version of the function)...i understand right?Ignaz
yeah thats enough for me) even if basically)Ignaz
(No -- the other way around!)Kingwood
just in case you are wondering you can use 'new self' or 'new static' instead of 'new self()' or 'new static()'.Bowery

© 2022 - 2024 — McMap. All rights reserved.