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:
What I'm curious about:
new self()
, I have to override/change all such cases, but maybe that's not a bad thing if my constructor changes.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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With