Closure On Constructor

../_images/closure_on_constructor.png

PHP 8.1 introduced first class callable, a syntax to build a closure by using the ellipis operator ... as argument. This works on all sorts of calls, methods, closure, functions.

The only call where it doesn’t work is the instantiation: PHP generates an error from that syntax.

On the other hand, it is possible to create a closure on the constructor method, like any other method. And there, it just calls again the constructor, on the object that was already created.

See Also

PHP Error Messages

PHP Features