I have a question regarding dependency injection. I still made it simple, my methodology is basically to exclude the creation of an object inside objects and pass it instead in the constructor. I have come to the point where I attack large classes that require multiple reflections. Some even have objects that contain other objects, with fun little singles here and there. It gets ugly when testing these classes, since they are far from being "isolated", they are still hardcoded depending on them.
So. Injecting an object or 2 for a trivial class is simple,
I looked at the dependency containers, saw a lot of implementations, and now I wonder what is the advantage of using a container over the registry, for example. Isn't it just as easy to use the registry to store anonymous functions that create the necessary dependencies when called?
The 2 containers I looked into, Php Dependency and Pimple are significantly different from the implementation.
I wonder about the benefits of using a container against direct objects. I do not understand how the implementation of php dependencies will be checked, i.e. How to implement a mock database object in phpunit without actually entering the class when testing? Is there any advantage in that the dependency was displayed and used in such dogs?
Class Book {
private $_database;
public function setDatabase($database) {
$this->_database = $database;
}
}
Pimple, , . docblock, , , - ....
Objects are defined by anonymous functions that return an instance of the object:
// define some parameters
$container['cookie_name'] = 'SESSION_ID';
$container['session_storage_class'] = 'SessionStorage';
..., factory:
$container['session'] = function ($c) {
return new Session($c['session_storage']);
};
(singleton!?):
$c['session'] = $c->share(function ($c) {
return new Session($c['session_storage']);
});
, , . BUt - ? Pimple, , , Php-Dependency .