Why return a new static? (Php)

Why do some people create one method that returns a new static element, and not the static methods of all methods? What is the reason that returns a new static ? I do not ask what is the difference between static and self, or that which is static and in itself. 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 make () method that returns a new static one . Then, some people call other methods as follows:

 $regex = Expression::make()->find('www'); 

What is his purpose? I see that here we are not using the new expression syntax, and if this is the point, why didn’t they make all the methods static, for example? What is the difference, what is the reason that this method returns a new static?

+7
oop php static class late-static-binding
source share
2 answers

new static creates a new object from the current class and works with late static bindings (creates an instance of a subclass, if the class has been subclassed, I expect you to understand that).

Having a static method for a class that returns a new instance is an alternative constructor. The value, usually your constructor, is a public function __construct , and usually it requires a specific group of parameters:

 class Foo { public function __construct(BarInterface $bar, array $baz = []) { ... } } 

Having an alternative constructor allows you to provide different default values ​​or convenient shortcuts to instantiate this class without having to provide these specific arguments and / or to provide different arguments that the alternative constructor will convert to canonical:

 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, although your canonical constructor requires a bunch of complex arguments, you can create a default instance of your class, which is likely to be good for most uses, just with Foo::getDefault() .

The canonical example in PHP for this is DateTime and DateTime::createFromFormat .

In your specific example, the alternate constructor does not actually do anything, so it is more likely to be superfluous, but I expect this because it is not a complete example. If there is an alternative constructor that does nothing but new static , this probably just means the convenient syntax over (new Foo)-> , which I find dubious.

+11
source share

get_called_class() ).

 class A { public static function get_self() { return new self(); } public static function get_static() { return new static(); } } class B extends A {} echo get_class(B::get_self()); // A echo get_class(B::get_static()); // B echo get_class(A::get_self()); // A echo get_class(A::get_static()); // A 
0
source share

All Articles