Hello devs, in this tutorial i will explain to you how we should use php composition over inheritance. We know that in object oriented programming, there are two ways using which classes can achieve polymorphic behaviour: “Inheritance” & “Composition”.
Inheritance is the concept of basing an object or class upon another object or class, retaining similar implementation. In another words, by using inheritance a class inherits properties from all its superclasses, whether direct or indirect. Inheritance establishes an “is-a” relationship between classes.
Composition is the mechanism to reuse code across classes by containing instances of other classes that implement the desired functionality. A composition establishes a “has-a” relationship between classes.
Let’s understand both Inheritance and Composition using some quick example and analyse why inheritance can be dangerous to implement in our application.
class Vehicle
{
public function move()
{
echo "Move the car";
}
}
class Car extends Vehicle
{
public function accelarate()
{
move();
}
}
$car = new Car();
$car->accelarate(); //Move the car
In above exaple, we’ve inherited class Vehicle
into the Car
class. It makes a very tight coupling between class Vehicle
and Car
. If anything gets changed in class Vehicle
, specifically in move()
method, class Car
can break easily as superclass Vehicle
have no idea of what child classes are uses it for.
This kind of tight coupling can be solved using composition. Let’s modify above example by composition to see that how composition can solve this issue.
class Vehicle
{
public function move()
{
echo "Move the car";
}
}
class Car
{
private $vehicle;
public function __construct(Vehicle $vehicle)
{
$this->vehicle = $vehicle;
}
public function accelarate()
{
$this->vehicle->move();
}
}
$vehicle = new Vehicle();
$car = new Car($vehicle);
$car->accelarate(); //Move the car
In the above example, we’re not using inheritance now. Instead of inheritance we’re using composition to achieve our required goal. Here, we’re now passing the class parameter as the reference of class Vehicle
into class Car
’s contructor using dependency injection.
So, we don’t depend on class Vehicle
because we can swap it out with another class very easily. And hence no tight coupling. Parent class and child class are highly independent from each other now.
Sometime we see that many object orientend programming doesn't support multiple inheritance. So for this type of situation we can easily avoid multiple inheritance condition using compostion. Cause if you use compostion, there is no need to use inheritance. You have to just inject your multiple class with constructor.
With composition, single inheritance languages such PHP, can easily overcome the lack of multiple inheritance. Have a look this example below.
class Vehicle
{
public function move()
{
echo "Move the car";
}
}
class Tire
{
public function addAlloys()
{
echo "Adding alloy wheels...";
}
}
class Car
{
private $vehicle;
private $tire;
public function __construct(Vehicle $vehicle, Tire $tire)
{
$this->vehicle = $vehicle;
}
public function accelarate()
{
$this->vehicle->move();
$this->tire->addAlloys();
}
}
As you can see, we have tweaked the previous example to use an another class Tyre
in class Car
. So, we’re now using two different classes into the Car
class. This wouldn’t be possible with inheritance.
Cause php doesn't support multiple inheritance. Especially when the language doesn’t support multiple inheritance. So we can easily overcome this problem by using composition.
#php #oop-php #inheritance #composition