I’m trying to create simple MVC skeleton and I’m stuck with dependencies.
This is what I have now:
$config = new Config();
$database = new Database($config);
$uri = new Uri('article/5');
$request = new Request($uri);
$response = new Response;
$router = new Router;
$dispatcher = new Dispatcher($request, $response, $router);
$dispatcher->dispatch(); // Routing, instantiate controller, execute action, send response
The question is: how can any object get access to any dependency?
Some examples:
- Controller may need Config to get output formatting options.
- Mapper may need Database to perform queries.
- Any Controller / Helper needs access to Log.
- Helper may need any number of dependencies (ex.:Uri_Helper needs Router).
The only possibility I can think of is to use Registry, but this violates Law of Demeter (ask what you really need).
You write factories(excellen article). This could be totally boring(like the article mentions) so you could use a DI-framework like for example:
Also I would like to point out that Misko’s blog is very interesting and has a lot of good reads on how to do testing properly. Especially the guide to writing testable code is a must read.
P.S: I think you should be writing factories, because PHP is a scripting language and you should use as little code as possible to make your site fast. That’s the problem with some PHP frameworks.
Rasmus Ledorf(PHP inventor) ‘s quote: