Liskov substitution principle

The Liskov substitution principle is one of the five SOLID principles of good programming. The general definition says that if A object is a subtype of B, and if the A object is treated as B, then it should behave like B object.

The classic example is a square and a rectangle. According to this principle: if a square is a subtype of a rectangle and a square is treated as a rectangle (means we use the formula for the area of a rectangle to a square), then we get the correct result.

In object-oriented programming Liskov principle sounds a little different: derived classes should not in any way affect the definition of the base class.

To better understand how the Liskov principle looks in object-oriented programming, first a classic example of violating of the Liskov rule:

class Rectangle {
    
    
    public $width, $height;
    
    
    public function setWidth($width)
    {
        $this->width = $width;
    }
    
    public function setHeight($height)
    {
        $this->height = $height;
    }
    
    
    public function area()
    {
        return $this->width * $this->height;
    }
    
}


class Square extends Rectangle  {
    
    
    public function setWidth($width)
    {
        $this->width = $width;
        $this->height = $width;
    }
    
    public function setHeight($height)
    {
        $this->height = $height;
        $this->width = $height;
    }
    
    
}


$obj = new Square; // object created somewhere in the code, for example by Dependency Injection pattern

// the beginning of a client code
if($obj instanceof Rectangle) // the condition is performed, and thus a violation of the definition of the base class (we are trying the rectangle to assign equal sides)
{
    $obj->setWidth(2);
    $obj->setHeight(3);
}


echo $obj->area(); // you will receive the result of 9 instead of 6, but the error may be difficult to notice in the case of more complex calculations

Become a Professionalist in Web Application Development! Enroll In The Course Of Laravel You will also learn how to create hybrid applications for smartphones

The problem here is incorrect inheritance that we should either get rid of or use an abstract superclass that other classes inherit both figures from. In other words we should inherit in a way that do not lead us to misunderstanding in the client code :

abstract class Shape {
    
    
    public $width, $height;
    
    
    public function setWidth($width)
    {
        $this->width = $width;
    }
    
    public function setHeight($height)
    {
        $this->height = $height;
    }
    
    
    public function area()
    {
        return $this->width * $this->height;
    }
    
}


class Rectangle extends Shape {}
    

class Square extends Shape  {
    
    
    public function setWidth($width)
    {
        $this->width = $width;
        $this->height = $width;
    }
    
    public function setHeight($height)
    {
        $this->height = $height;
        $this->width = $height;
    }
    
    
}


$obj = new Square;  // object created somewhere in the code, for example by Dependency Injection pattern

// the beginning of a client code
if($obj instanceof Rectangle)
{
    $obj->setWidth(2);
    $obj->setHeight(3);
}


echo $obj->area(); // now we get 0 because above condition is not performed, the programmer realizes that something is wrong

 

enroll in the course of Laravel framework >>>> or affiliate program
Share

Leave a Reply

Your email address will not be published. Required fields are marked *