Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use new self or new static?

I work on a proprietary project that uses quite a lot of factories of one form or another. Most of them don't instantiate the class by name, fortunately, but whether new self() or new static() is used to instantiate varies depending on the developer.

I'm aware of the difference, but I'm curious if there's some consensus on which one is the "correct" way to go when late static binding isn't technically required. For instance, new static() is often found in service classes that will almost certainly never be subclassed. It's obviously important in abstract classes, but my preference would be to use new self() where I don't expect a subclass.

Questions that address the technical difference:

  • New self vs. new static
  • what means new static?
  • Why return new static? (PHP)

What I'm curious about:

  • Is there a performance hit to using late static binding?
  • Are there code maintenance implications to adopting one practice? Eg. If I subclass a class using new self(), I have to override/change all such cases, but maybe that's not a bad thing if my constructor changes.
  • Is there a documented best practice on this? We use PSR-2, at least aspirationally, but I don't think it covers this.
like image 835
Mikkel Avatar asked Oct 20 '17 13:10

Mikkel


People also ask

What is the difference between static and self?

self Vs static: The most basic difference between them is that self points to the version of the property of the class in which it is declared but in case of static, the property undergoes a redeclaration at runtime.

What is new static?

New static: The static is a keyword in PHP. Static in PHP 5.3's late static bindings, refers to whatever class in the hierarchy you called the method on. The most common usage of static is for defining static methods.

What is the difference between using self and this?

The keyword self is used to refer to the current class itself within the scope of that class only whereas, $this is used to refer to the member variables and function for a particular instance of a class.

What is the difference between using self and this PHP?

self is used to access static or class variables or methods and this is used to access non-static or object variables or methods. So use self when there is a need to access something which belongs to a class and use $this when there is a need to access a property belonging to the object of the class.


1 Answers

First, lets discuss the difference for a moment:

Returning new static will result in an instance of the derived class, meaning if Foo declares static method Create() (a factory method) that returns a new static, then calling Foo::Create() or self::Create() from Foo will return an instance of Foo. However, if class Bar extends Foo, and you call Bar::Create(), it'll return an instance of Bar.

Thus, if you want Bar::Create() to return an instance of Foo, you should use new self, not new static.

Considering developer expectations that static factory methods such as Create() return instances of the derived classes, I'd argue that returning the late-bound type is likely the right answer in most circumstances. However if a class is marked final (sealed, closed to extension), then it doesn't matter. Assuming a class isn't final, (which presumes others are allowed to extend it), static is likely the right choice.

As far as performance goes; given PHP is interpreted, I can't imagine it has significant performance implications. This is really a discussion about what is more correct/semantic, and to reiterate, I think returning a new static is the correct answer most of the time, with returning a new self being the exception in specific circumstances.

Note that this question is actually pretty similar to here: Is it possible to overuse late static binding in PHP?. You may find that answer useful, too.

like image 126
MSC Avatar answered Sep 24 '22 14:09

MSC