mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-14 22:30:27 +08:00
Fixes #9331: betted PHP Callable Injection example
This commit is contained in:
@ -95,29 +95,25 @@ $container->set('Foo', function () {
|
|||||||
$foo = $container->get('Foo');
|
$foo = $container->get('Foo');
|
||||||
```
|
```
|
||||||
|
|
||||||
To hide the complex logic for building a new object, you may use a static class method to return the PHP
|
To hide the complex logic for building a new object, you may use a static class method as callable. For example,
|
||||||
callable. For example,
|
|
||||||
|
|
||||||
```php
|
```php
|
||||||
class FooBuilder
|
class FooBuilder
|
||||||
{
|
{
|
||||||
public static function build()
|
public static function build()
|
||||||
{
|
{
|
||||||
return function () {
|
$foo = new Foo(new Bar);
|
||||||
$foo = new Foo(new Bar);
|
// ... other initializations ...
|
||||||
// ... other initializations ...
|
return $foo;
|
||||||
return $foo;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$container->set('Foo', FooBuilder::build());
|
$container->set('Foo', ['app\helper\FooBuilder', 'build']);
|
||||||
|
|
||||||
$foo = $container->get('Foo');
|
$foo = $container->get('Foo');
|
||||||
```
|
```
|
||||||
|
|
||||||
As you can see, the PHP callable is returned by the `FooBuilder::build()` method. By doing so, the person
|
By doing so, the person who wants to configure the `Foo` class no longer needs to be aware of how it is built.
|
||||||
who wants to configure the `Foo` class no longer needs to be aware of how it is built.
|
|
||||||
|
|
||||||
|
|
||||||
Registering Dependencies <span id="registering-dependencies"></span>
|
Registering Dependencies <span id="registering-dependencies"></span>
|
||||||
|
Reference in New Issue
Block a user