So I'm developing a framework and up until now I've been using a container that can be called statically throughout the application like so:
$router = new Router;
Container::set('router', $router); // this can now be called anywhere
This then in any other file within the app, can be called like so:
$router = Container::get('router'); // get the router object with all current data
$router->add_route('/blog', '/Path/To/Controller/Blog@index');
Within the controller I would then have:
class Blog_Controller extends Base_Controller
{
public function __construct()
{
$this->blog_model = Container::get('Blog_Model'); // this is instantiated OR if it has already, the container returns the existing object INCLUDING all of its properties thus far.
}
public function view($id)
{
$this->blog_model->get_post($id);
}
}
Though, I've read up on how to create DI Containers that inject the dependencies when the controller is called like in the example below:
class Blog_Controller extends Base_Controller
{
public function __construct(Blog_Model $blog_model)
{
$this->blog_model = $blog_model; // this is instantiated OR if it has already, the container returns the existing object INCLUDING all of its properties thus far.
}
public function view($id)
{
$this->blog_model->get_post($id);
}
}
I'm having trouble figuring out how to effectively do so.
How would I get Blog_Model to be instantiated (or returned if it already has been) without having to manually do new Blog_Model
.