Low Budget, Dependency Injection Container. Suitable for all ages!
Container69 for PHP is a really, really simple Dependency Injection Container / Inversion of Control Container (what is this?)
###How to work this magic Include the file Cotainer69.php in your PHP script and initialize it
include "Container69.php";
$container = new Container();Before you can get an object from the container you must register it, to keep it real simple there is no automatic registry in Container69 this means if you want to use an object it has to have already been prepared for use with the register method.
$container->register($class, $constructor_args, $dependency_function);$class The class name to register
$constructor_args (optional) Array of arguments that will be passed to the class constructor
$dependency_function (optional) A simple lambda function so you can define post actions that can happen after the constructor has been called, for example use this to call a method that will be required
Here's the old car example in action
$container->register("Car", array("Fiat", "Punto"), function($obj) {
$obj->set_colour("Purple");
});Here we have a class called "Car" and the constructor for this class asks for two arguments, make and model. You can see this in action in the example above, the lambda function then calls a method called set_colour in the class "Car".
Now that we have registered the class we can get an object of this class from the container,
$container->get($class, $constructor_args, $dependency_function);The arguments here are the same as the ones for the "register" method, $constructor_args and $dependency_function are optional, if you do not specify these (which most of the time you probably wont) then then the ones used in the "register" method will be used. Back to the car example,
$my_car = $container->get("Car");
echo $my_car->make_car();You should be able to see here that all we are doing is getting an object of the class "Car" from the container, if we wanted to use alternate constructor or post constructor methods then we can overwrite the ones used in the "register" method by specifying them here. ###The Car Class
class Car {
protected $make;
protected $model;
protected $colour;
function __construct($make, $model) {
$this->make = $make;
$this->model = $model;
}
public function set_colour($colour) {
$this->colour = $colour;
}
public function make_car() {
return "
Vehical Make: " . $this->make . "<br />
Model: " . $this->model . "<br />
With colour: " . $this->colour . "<br />
Has been made!";
}
}###That's it
The full example is included in this repo.
evaheb ,edoc fo senil 96 sniatnoc 96 reniatnoc
