IoC container concept in PHP
NickName:PayamB Ask DateTime:2015-09-14T22:57:40

IoC container concept in PHP

I'm trying to grasp the concept of IoC and Dependency Injection in PHP, So iv'e decided to go on and create a small framework and implement these design patterns.

i've created my own IoC class which you can find here https://gist.github.com/payamb/d8134ae0c5d09cda79ff

And i feed my configs like this to the IoC container at run time

 // --- Main Application
    [
        'name'      =>  'Application',
        'service'   =>  [
            'type'      =>  'constructor',
            'class'     =>  'Pebbles\Core\Application',
            'params'    =>  [
                'router'    =>  '*AltoRouter',
                'request'   =>  '*Request',
                'IoC'       =>  '*IoC'
            ],
            'call'      => [
                [
                    'method'    =>  'setRoutes',
                    'args'      =>  [$routes]
                ]
            ]
        ],
        'lazy'      =>  FALSE
    ]

And the IoC class handle the dependency injection and initiliaztion.

By doing this i got my head around what actually IoC does and why is it important.

Here is how my front controller looks like :

// read $params from config file and pass it to IoC
$container = new \Pebbles\Core\IoC($params);
$container->getService('Application')->setEnvironment('dev')->run();

And this is the my Application class :

namespace Pebbles\Core;
use Symfony\Component\HttpFoundation\Request;
class Application
{
    protected $request;
    protected $router;
    protected $container;
    protected $environment;
    public function __construct(\AltoRouter $router, Request $request, IoC $container)
    {
        $this->request = $request;
        $this->router = $router;
        $this->container = $container;
    }
    public function setEnvironment($env)
    {
        $this->environment = $env;
        return $this;
    }
    public function run()
    {
        $match = $this->router->match();
        if ($match) {
            list ($controller, $action) = explode(':', $match['target']);
            $object = & $this->container->getService($controller);
            try {
                call_user_func_array([$object, $action], $match['params']);
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }
    }

But now i'm struggeling how to move forward from this point. Do i have to create a config file for all of controllers and services i'm going to use in this project and feed them to IoC (lazy) ? I mean if i'm going to have a bookingController, i have to create a config file for that and feed it to container in order to be able to use and route request to it.

but what if i have like 20 controller and 10 services ? is it still the right approach ? i'm not sure about that bit that injected the IoC itself to Application class, but how else can i route requests if i don't have access to IoC container.

Am i making mistake in implementing IoC desgin pattern ?

Here is the github link to what my actual code looks like https://github.com/payamb/Pebbles

Copyright Notice:Content Author:「PayamB」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/32567963/ioc-container-concept-in-php

More about “IoC container concept in PHP” related questions

IoC container concept in PHP

I'm trying to grasp the concept of IoC and Dependency Injection in PHP, So iv'e decided to go on and create a small framework and implement these design patterns. i've created my own IoC class whi...

Show Detail

JSP Implicit Objects are created by the JSP Container. Is this an example of IOC concept?

The implicit objects for a JSP are created by the JSP Container. Is this an example of IOC Concept?

Show Detail

why use ioc container in php applications?

I already know what is IOC and also I have done some PHP projects with Laravel (4.3) framework which is IOC based. also I know ZEND (1.12) and have done some projects with it. I know how ZEND MVC s...

Show Detail

Where to put libraries for the IoC Container to work in Laravel?

The IoC container is kind of a complex subject for me, and coming from a Codeigniter background (where you just could copy and paste a library and it worked by including it with include_once()), it...

Show Detail

IoC Container PHP Routing

I have recently started working on my own MVC PHP application. Currently I am trying to set everything up in a way that I would consider "clean". After some reading I came across IoC containers and

Show Detail

What makes an IoC container an IoC container?

So..I've been digging into IoC container and service locator. I thought an IoC container is an IoC container, not a service locator because The way you use it. You pass a service locator to a class

Show Detail

How to organize MVP with an IoC container?

I'm trying to get the IoC concept down with a winforms app. Say I have a presenter whose constructor takes its view and a service as constructor arguments. So in the form code I have something that

Show Detail

php ioc container within plugin

I am writing a plugin for a content management system that does not use a ioc container. Is is good practise to use my own ioc container like Pimple within my plugin to manage all my classes etc e...

Show Detail

How to make your own PHP IoC/ DIC based on how Laravel works?

I am interested in writing my own IoC/ DIC for PHP based on the way Laravel works. This is purely for learning as it interests me and I want to understand the concept better. I've been reading the

Show Detail

IoC Container and global variable

I see advise from a lot of people teach not to use global variable/singleton/static class and move to use of a iOC container, for example, in PHP larvel framework, it is App::bind('foo', function(...

Show Detail