Why some developers create one method that returns new static? What is the reason to have a method that returns new static? I am not asking what is the difference between static and self, or what static & self mean. For example, here is one simple class:
<?php class Expression { public static function make() { return new static; } public function find($value) { return '/' . $value .'/'; } public function then($value) { return $this->find($value); } public function hi($value) { return "hi"; } }
As you can see, there is a static method make() which returns new static. Then, some developers call other methods like this:
$regex = Expression::make()->find('www');
What is the purpose of this? I see that here we don't use new Expression syntax, and if that's the point - then why not make all methods static? What is the difference, what is the reason to have that one method that returns new static (while other methods are not static)?
new static instantiates a new object from the current class, and works with late static bindings (instantiates the subclass if the class was subclassed, I expect you understand that). Having a static method on a class which returns a new instance of same is an alternative constructor.
The static keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class. The static keyword is also used to declare variables in a function which keep their value after the function has ended.
static return type follows Liskov Substitution Principle. A child class method can return a narrower class object than the parent methods return type. Because static always refer to the class name of the called object (i.e. same as get_class($object) ), static is a subset of self , which in turn is subset of parent .
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
instantiates a new object from the current class, and works with late static bindings (instantiates the subclass if the class was subclassed, I expect you understand that).
Having a static
method on a class which returns a new instance of same is an alternative constructor. Meaning, typically your constructor is public function __construct
, and typically it requires a certain bunch of parameters:
class Foo { public function __construct(BarInterface $bar, array $baz = []) { ... } }
Having an alternative constructor allows you to provide different defaults, or convenience shortcuts to instantiate this class without having to supply those specific arguments and/or for being able to provide different arguments which the alternative constructor will convert to the canonical ones:
class Foo { public function __construct(BarInterface $bar, array $baz = []) { ... } public static function fromBarString($bar) { return new static(new Bar($bar)); } public static function getDefault() { return new static(new Bar('baz'), [42]); } }
Now, even though your canonical constructor requires a bunch of complex arguments, you can create a default instance of your class, which will probably be fine for most uses, simply with Foo::getDefault()
.
The canonical example in PHP for this is DateTime
and DateTime::createFromFormat
.
In your concrete example the alternative constructor doesn't actually do anything, so it's rather superfluous, but I expect that's because it's an incomplete example. If there's indeed an alternative constructor which does nothing other than new static
, it's probably just meant as convenience syntax over (new Foo)->
, which I find questionable.
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