Understanding the IoC container

Posted 2 years ago by fsdolphin

Hi,

I'm trying to have a better understanding on how the IoC container works and what are the benefits of using it. The lack of understanding these concepts it's stopping me from creating more complex projects and learn more advance concepts.

How would the following code benefit from using the IoC container?

class Car
{
    private $color;
    private $engine;

    public function __construct(Color $color, Engine $engine)
        {
            $this->color = $color;
            $this->engine = $engine;
        }

        public function makeCar()
        {
            // creating car...
         }
}

$jetta =  new Car(red, 2.5);

$jetta->makeCar();

If the code above is not sufficient for demonstrating the power of using IoC container, could someone create an example using plain PHP vs using the IoC container?

Thanks a lot!

Please sign in or create an account to participate in this conversation.