Professional Documents
Culture Documents
Permalink: https://linuxhint.com/inversion-control-php/
category: 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.
<?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>";
}
}
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.
<?php
//Define ClassA
class ClassA
{
public $ClassB;
public $ClassC;
//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>";
}
}
?>
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.
<?php
//Define ClassA
class ClassA
{
public $Class_obj;
//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>";
}
}
?>
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.