php

Inversion of control in PHP

Inversion of control (IOC) defines the way objects are used, but it does not specify how to create them. IOC defines the relationship between the high-level class and detail class, where the high-level class depends on detail class. High- and low-level classes are related by abstracting a detail class in IOC. In this tutorial, we explain how to implement IOC in PHP.

Example 1: Implement IOC using the method function

To follow along with this example, create a PHP file with the following script. Here, ClassA depends on ClassB and ClassC. If ClassB or ClassC are modified, then ClassA will be affected. If ClassB and ClassC are developed by different programmers, then a dependency problem will arise. This problem can be solved by IOC. In the script, two variables are declared to act as the object variables of ClassB and ClassC. When the method() function of ClassA is called, then $ClassB and $ClassC will be initialized, and the method() functions of ClassB and ClassC will be called.

<?php
//Define ClassA
class ClassA
{
    public $ClassB;
    public $ClassC;
    public function ClassA()
    {
        echo "<h2 style='color:red'> Initialization of ClassA </h2>";
    }
    public function method()
    {
        $this->ClassB = new ClassB();
        $this->ClassC = new ClassC();
        $this->ClassB->method();
        $this->ClassC->method();
    }
}

//Define ClassB
class ClassB
{
    public function ClassB()
    {
        echo "<h2 style='color:blue'> Initialization of ClassB </h2>";
    }
    public function method()
    {
        echo "<h3 style='color:blue'> The output from ClassB </h3>";
    }
}

//Define ClassC
class ClassC
{
    public function ClassC()
    {
        echo "<h2 style='color:green'> Initialization of ClassC </h2>";
    }
    public function method()
    {
        echo "<h3 style='color:green'> The output from ClassC </h3>";
    }
}

//Create object of ClassA
$object = new ClassA();
//Call the method() function
$object->method();
?>

Output:

The following output will be produced by running the above script from the webserver. Here, the constructor of the three classes is called when ClassA is created. Next, the method() functions of ClassB and ClassC are called to generate the output.

Example 2: Implement IOC using constructors

To follow this example, create a PHP file with the following script. Here, the constructor of ClassA depends on ClassB and ClassC. ClassB and ClassC are passed as arguments to the constructor of ClassA. ClassB and ClassC will be created outside ClassA. Therefore, if anything is modified in ClassB or ClassC, then no changes are required in ClassA. A formatted text message will be printed from the constructor of ClassB and ClassC. The method() functions of ClassB and ClassC are called from the method() function of ClassA.

ClassB = $b;
        $this->ClassC = $c;
    }
    public function method()
    {
        $this->ClassB->method();
        $this->ClassC->method();
    }
}


//Define ClassB
class ClassB
{
    public function ClassB()
    {
        echo "<h2 style='color:blue'> Initialization of ClassB </h2>";
    }
    public function method()
    {
        echo "<h3 style='color:blue'> The output from ClassB </h3>";
    }
}

//Define ClassC
class ClassC
{
    public function ClassC()
    {
        echo "<h2 style='color:green'> Initialization of ClassC </h2>";
    }
    public function method()
    {
        echo "<h3 style='color:green'> The output from ClassC </h3>";
    }
}

//Create object of ClassA
$object=new ClassA(new ClassB(), new ClassC());
//Call the method() function
$object->method();

?>

Output:

The following output will be produced by running the above script from the webserver. Here, the first two lines in the output were generated from the constructor of ClassB and ClassC. Then, the method() functions of ClassB and ClassC were called, and the output was generated.

Example 3: Implement IOC using inheritance

To follow this example, create a PHP file with the following script. Here, ClassA and ClassB are unrelated, and childClass1 and childClass2 inherit ClassB. The constructor of ClassA takes the object of another class as an argument and initializes the class variable, $Class_obj. This class variable is used to call the method() function of the class object that is used in the constructor of ClassA.

Class_obj = $obj;
    }
    public function method()
    {
        $this->Class_obj->method();
    }
}

//Define ClassB
class ClassB
{
    public function method()
    {
        echo "<h3 style='color:blue'> The output from ClassB </h3>";
    }
}

//Define childClass1
class childClass1 extends ClassB
{
    public function method()
    {
        echo "<h3 style='color:blue'> The output from childClass1</h3>";
    }
}

//Define childClass2
class childClass2 extends ClassB
{
    public function method()
    {
        echo "<h3 style='color:red'> The output from childClass2</h3>";
    }
}

//Create the object of ClassA
$object = new ClassA(new childClass1());
//Call the method() function
$object->method();

?>

Output:

The following output will be produced by running the above script from the webserver. The object of childClass1 was passed as an argument when creating ClassA, which is a child class of ClassB. Here, it is not necessary to know from which class childClass1 is inherited. After initializing ClassA, the class variable $Class_obj called the method() function of childClass1 and printed the text as output.

Conclusion

IOC is an advanced object-oriented programming concept. In this tutorial, we showed you how to implement IOC in PHP by using some simple examples.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.