75

I saw in some frameworks this line of code:

return new static($view, $data);

how do you understand the new static?

hakre
  • 184,866
  • 48
  • 414
  • 792
Hello
  • 2,179
  • 4
  • 22
  • 27
  • What framework was this? – Aleks G Apr 09 '13 at 09:55
  • for example laravel, but i found that only in the core, not while using their api – Hello Apr 09 '13 at 09:56
  • 2
    Check this page for more info; http://stackoverflow.com/questions/5197300/new-self-vs-new-static –  Apr 09 '13 at 09:57
  • [PHP late static binding](http://php.net/manual/en/language.oop5.late-static-bindings.php) – Alvin Wong Apr 09 '13 at 09:57
  • @Allendar: So it's like `decltype(*this)` with polymorphism disabled? What horrible keyword usage! – Lightness Races in Orbit Apr 09 '13 at 10:00
  • @Lightness *"It was decided not to introduce a new keyword but rather use `static` that was already reserved."* http://php.net/manual/en/language.oop5.late-static-bindings.php – deceze Apr 09 '13 at 10:02
  • so shortly said, its just calling the constructor of the current class, as far i understand – Hello Apr 09 '13 at 10:03
  • @Hello If by *current* you mean *the class in which `static` is used or the current subclass of it*, then yes. – deceze Apr 09 '13 at 10:04
  • @LightnessRacesinOrbit: No, it's exactly like `decltype(*this)`. The polymorphism-disabled version would be `new self`. – Jon Apr 09 '13 at 10:05
  • @Jon FYI, [`decltype` is already "polymorphism-disabled"](http://stackoverflow.com/a/10424417/560648). As a static construct it seems to be like `static` here to me. `self` would be a version that employs RTTI. – Lightness Races in Orbit Apr 09 '13 at 10:10
  • @Jon http://stackoverflow.com/q/15899369/560648 – Lightness Races in Orbit Apr 09 '13 at 10:24
  • @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. – Jon Apr 09 '13 at 10:28
  • @Jon: And I was wrong. :) – Lightness Races in Orbit Apr 09 '13 at 10:48
  • 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. – Coder Guy May 13 '15 at 04:47

1 Answers1

127

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.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 369,052
  • 73
  • 620
  • 1,021