no-framework-tutorial/7-inversion-of-control.md

51 lines
2.3 KiB
Markdown
Raw Normal View History

2014-09-20 17:56:08 +00:00
[<< previous](6-dispatching-to-a-class.md) | [next >>](8-dependency-injector.md)
2014-09-16 19:48:41 +00:00
### Inversion of Control
2014-11-30 20:06:11 +00:00
In the last part you have set up a controller class and generated output with `echo`. But let's not forget that you have a nice object oriented HTTP abstraction available. But right now it's not accessible inside your class.
2014-09-16 19:48:41 +00:00
The sane option is to use [inversion of control](http://en.wikipedia.org/wiki/Inversion_of_control). This means that instead of giving the class the responsiblity of creating the object it needs, you just ask for them. This is done with [dependency injection](http://en.wikipedia.org/wiki/Dependency_injection).
If it sounds a little complicated right now, don't worry. Just follow the tutorial and once you see how it is implemented things will make more sense.
2014-11-30 20:06:11 +00:00
Change your `Homepage` controller to the following:
2014-09-16 19:48:41 +00:00
2014-09-17 20:06:16 +00:00
```php
2014-09-16 19:48:41 +00:00
<?php
2014-11-30 20:06:11 +00:00
namespace Example\Controller;
2014-09-16 19:48:41 +00:00
use Http\Response;
2014-11-30 20:06:11 +00:00
class Homepage
2014-09-16 19:48:41 +00:00
{
private $response;
public function __construct(Response $response)
{
$this->response = $response;
}
2014-11-30 20:06:11 +00:00
public function show()
2014-09-16 19:48:41 +00:00
{
$this->response->setContent('Hello World');
}
}
```
2014-11-30 20:06:11 +00:00
Note that you are [importing](http://php.net/manual/en/language.namespaces.importing.php) `Http\Response` at the top of the file. This means that whenever you use `Response` inside this file, it will resolve to the fully qualified name.
2014-09-16 19:48:41 +00:00
2014-09-16 19:53:11 +00:00
In the contructor you are now explicitly asking for a `Http\Response`. In this case, `Http\Response` is an interface. So any class that implements the interface can be injected. See [type hinting](http://php.net/manual/en/language.oop5.typehinting.php) and [interfaces](http://php.net/manual/en/language.oop5.interfaces.php) for reference.
2014-09-16 19:48:41 +00:00
Now the code will result in an error because you are not actually injecting anything. So let's fix that in your `Bootstrap.php` where you dispatch when a route was found:
2014-09-17 20:06:16 +00:00
```php
2014-09-16 19:48:41 +00:00
$class = new $className($response);
$class->$method($vars);
```
The `Http\HttpResponse` object implements the `Http\Response` interface, so it fulfills the contract and can be used.
2014-11-16 19:04:42 +00:00
Now everything should work again. But if you follow this example, all your objects that are instantiated this way will have the same objects injected. This is of course not good, so let's fix that in the next part.
2014-09-16 19:48:41 +00:00
2014-09-20 17:56:08 +00:00
[<< previous](6-dispatching-to-a-class.md) | [next >>](8-dependency-injector.md)