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.
deceze
source share