mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-14 06:11:35 +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');
|
||||
```
|
||||
|
||||
To hide the complex logic for building a new object, you may use a static class method to return the PHP
|
||||
callable. For example,
|
||||
To hide the complex logic for building a new object, you may use a static class method as callable. For example,
|
||||
|
||||
```php
|
||||
class FooBuilder
|
||||
{
|
||||
public static function build()
|
||||
{
|
||||
return function () {
|
||||
$foo = new Foo(new Bar);
|
||||
// ... other initializations ...
|
||||
return $foo;
|
||||
};
|
||||
$foo = new Foo(new Bar);
|
||||
// ... other initializations ...
|
||||
return $foo;
|
||||
}
|
||||
}
|
||||
|
||||
$container->set('Foo', FooBuilder::build());
|
||||
$container->set('Foo', ['app\helper\FooBuilder', 'build']);
|
||||
|
||||
$foo = $container->get('Foo');
|
||||
```
|
||||
|
||||
As you can see, the PHP callable is returned by the `FooBuilder::build()` method. By doing so, the person
|
||||
who wants to configure the `Foo` class no longer needs to be aware of how it is built.
|
||||
By doing so, the person 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>
|
||||
|
Reference in New Issue
Block a user