-
-
Notifications
You must be signed in to change notification settings - Fork 311
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from mnapoli/3.3
3.3
- Loading branch information
Showing
32 changed files
with
769 additions
and
247 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
.DS_Store | ||
nbproject/* | ||
.idea/* | ||
*.iml | ||
vendor/* | ||
composer.phar | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Inject on an existing instance | ||
|
||
*Feature introduced in PHP-DI 3.3* | ||
|
||
The standard way of using a container is to get an object from it, with all its dependencies injected: | ||
|
||
```php | ||
$object = $container->get('foo'); | ||
``` | ||
|
||
But in some situations, you don't have the control of the creation of an object, yet **you want to resolve its dependencies**. | ||
|
||
PHP-DI offers the `injectOn` method: | ||
|
||
```php | ||
// $object is an instance of some class | ||
|
||
$container->injectOn($object); | ||
``` | ||
|
||
Now, `$object` has all its dependencies injected (through setter injections and property injections). | ||
|
||
|
||
## Constructor injection | ||
|
||
PHP-DI will not perform any constructor injection (because the instance is already created). | ||
|
||
If you create the object yourself, you'll have to do the constructor injection yourself: | ||
|
||
```php | ||
$object = new MyClass($someDependency, $someOtherDependency); | ||
$container->injectOn($object); | ||
``` | ||
|
||
If you get the object from some library/framework, then just call `injectOn()` | ||
|
||
|
||
### Why? | ||
|
||
Hopefully, that will help to integrate PHP-DI with other frameworks: | ||
|
||
- **MVC frameworks** (Symfony 2, Zend Framework 2, …): inject dependencies of the controller, in the controller itself. | ||
- **Tests** (PHPUnit, …): inject tools in your test class, for example a logger, a timer (for performance test), **the entity manager** (for integration tests), … | ||
|
||
Example: | ||
|
||
```php | ||
class MyController { | ||
public function __construct() { | ||
// get container ... | ||
$container->injectOn($this); | ||
} | ||
} | ||
``` | ||
|
||
**Of course**, the preferred method is still to use `$container->get()`. But sometimes you can't get to the root of the framework to intercept the creation of your objects. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# PHP-DI 3.3 released | ||
|
||
*Posted by [Matthieu Napoli](https://github.com/mnapoli) on July 30rd 2013* | ||
|
||
I am happy to announce that PHP-DI version 3.3 has just been released. | ||
|
||
The major new feature is the possibility to inject all dependencies of **an existing instance**. | ||
|
||
But before jumping into that, did you know there's an article on [how PHP-DI works](../doc/how-it-works.md). | ||
|
||
## Container::injectOn() | ||
|
||
*Read the full documentation [here](../doc/inject-on-instance.md)* | ||
|
||
```php | ||
$myObject = new MyClass(); | ||
|
||
$container->injectOn($myObject); | ||
``` | ||
|
||
Now, `$myObject` has all its dependencies injected (setter injections and property injections). | ||
|
||
It is basically the same as `$myObject = $container->get('MyClass')` except **you** create the instance. | ||
|
||
### Why? | ||
|
||
Weird right? Well it's not supposed to be used everywhere. | ||
|
||
Hopefully, that will help **a lot** to integrate PHP-DI with other frameworks: | ||
|
||
- **MVC frameworks** (Symfony 2, Zend Framework 2, …): inject dependencies of the controller, in the controller itself. | ||
- **Tests** (PHPUnit, …): inject tools in your test class, for example a logger, a timer (for performance test), **the entity manager** (for integration tests), … | ||
|
||
Example: | ||
|
||
```php | ||
class MyController { | ||
public function __construct() { | ||
// get container ... | ||
$container->injectOn($this); | ||
} | ||
} | ||
``` | ||
|
||
**Of course**, the preferred method is still to use `$container->get()`. But sometimes you can't get to the root of the framework to intercept the creation of your objects (I'm looking at you most MVC frameworks, PHPUnit & …) | ||
|
||
### Good to know | ||
|
||
PHP-DI will not perform any constructor injection. But of course most of the time you'll be using that feature is when you can't/don't want to use the constructor for getting your dependencies. | ||
|
||
|
||
## Change log | ||
|
||
Except this feature, 3.3 contains an optimization for definition resolution and a bugfix for a rare situation. | ||
|
||
Read all the changes and their authors in the [change log](../change-log.md). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# News | ||
|
||
- July 23rd 2013. [PHP-DI 3.2 released](02-php-di-3-2.md) | ||
- July 30th 2013. [PHP-DI 3.3 released](03-php-di-3-3.md): Inject dependencies into an existing instance and more… | ||
|
||
- June 23rd 2013. [PHP-DI 3.1 released](01-php-di-3-1.md) | ||
- July 23rd 2013. [PHP-DI 3.2 released](02-php-di-3-2.md): Lazy injection, `ContainerBuilder` and more… | ||
|
||
- June 23rd 2013. [PHP-DI 3.1 released](01-php-di-3-1.md): Zend Framework 1 integration and more… |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.