- If a class implements the `__invoke()` method it is a `callable-` or `invokable-object`.
- Its instance can be used as an anonymous function, callable or closure.
- `__invoke()` implements the main (or the only) functionality of the object.
- Why not to use `$instance->myMethod()` instead? You would need to implement an API and others would have to know it. Calling `$instance()` is a "universal anonymous API". Plus modern middleware or handlers often need to be passed as "callables".
- Usual anonymous functions can only do a simple task. When you use `$instance()`, you are backed with a large object which can do much more. It can also use traits, state or OOP benefits.
- Method `__invoke($a ,$b)` can take input parameters. But the application must know about them, which brings me back to interfaces. I am confused a little. So the invoke-params should probably be mostly provided by the DI I guess.
- But you still can use method instead of invokation. For example in file `config/common/routes.php` you can use both:
- Invokable objects are often used for middleware as it fits naturally into dispatcher and pipeline systems. Middleware can then be stateful. But the same aplies to interface-based approaches, you only need to specify the method.
## Hash annotations for class attributes
PHP 8 introduces annotations like this (not only for class attributes):