Fixes #9331: betted PHP Callable Injection example

This commit is contained in:
Alexander Makarov
2015-09-05 01:53:20 +03:00
parent 6b2748a0b3
commit 112fc83f27

View File

@ -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>