Revision #37 was created by rackycz on Oct 14, 2025, 12:27:43 PM.
edit
Content
[...]
First of all, learn what [PHP Standards Recommendations](https://en.wikipedia.org/wiki/PHP_Standard_Recommendation) by [Framework Interoperability Group (FIG)](https://www.php-fig.org/psr) are. It will help you understand why so many "weird" PSR imports are in the Yii3 code. In short: These interfaces help authors of different frameworks to write compatible classes so they can be reused in any other framework following these principles.
## Cconstruct vs I() and invoke()
It may be confusing that some classes contain both methods. Note that "property promotion" should be only used in `__construct()`.
Magic method `__invoke()` is only used if you create an instance and then use it as a function. Example if the reader does not have experiences with invoking. Just like me a few days ago. The `__invoke()` is called when you call the **instance** as a method. Like this:
```php
$obj = new MyObj(); // Now the __construct() is executed.
$obj(); // Now the __invoke() is executed
```
To be honest, I still do not fully understand the real purpose of this situa (The instance is needed!)
```
I never used it, but prepared a following example that shows when invoking can be applied:
```php
class MyUpper
{
public function __invoke($a) { return $this->go($a); }
public function go($a) { return strtoupper($a); }
}
$instance = new MyUpper();
$array = ['a', 'B', 1, '1'];
var_dump($instance($array[0]));
var_dump(array_map($instance, $array)); // __invoke is used
// These do the same:
var_dump(array_map('strtoupper', $array));
var_dump(array_map([$instance, 'go'], $array));
var_dump(array_map(function($a) use ($instance) { return $instance->go($a); }, ['a','B',1,'1']));
```
Note that "property promotion" should be only used in `__construct()` just like the dependency injection.
## Hash annotations for class attributes
PHP 8 introduces annotations like this (not only for class attributes):