Understanding the Liskov Substitution Principle with PHP
Introduction

The Liskov Substitution Principle (LSP) is a crucial concept in object-oriented programming that ensures the correct behavior of derived classes when used interchangeably with their base classes. By adhering to LSP, developers can create a more robust and maintainable codebase. In this article, we’ll explore LSP and demonstrate its implementation using PHP 8 and PSR (PHP Standards Recommendations) best practices.

What is the Liskov Substitution Principle?

The Liskov Substitution Principle, named after computer scientist Barbara Liskov, is one of the SOLID principles of object-oriented design. It states that objects of a superclass should be replaceable with objects of its subclasses without affecting the correctness of the program. In other words, a derived class should be able to substitute its base class without causing unexpected behavior or breaking the program’s functionality.

The essence of LSP lies in ensuring that the derived class honors the contract defined by the base class and doesn’t alter or weaken any preconditions, postconditions, or invariants established by the base class.

Example Scenario: Geometric Shapes

Let’s consider a practical example involving geometric shapes. We’ll create a base class called Shape, which will define the common behavior of all shapes, and two derived classes: Rectangle and Square.

Step 1: Defining the Shape class and PSR Interface
<?php

// Shape.php - Base class defining the contract for all shapes
interface Shape
{
    public function area(): float;
}
Step 2: Implementing the Rectangle Class
<?php

// Rectangle.php - Implementing the Rectangle class
class Rectangle implements Shape
{
    protected $width;
    protected $height;

    public function __construct(float $width, float $height)
    {
        $this->width = $width;
        $this->height = $height;
    }

    public function area(): float
    {
        return $this->width * $this->height;
    }
}
Step 3: Implementing the Square Class
<?php

// Square.php - Implementing the Square class
class Square implements Shape
{
    protected $side;

    public function __construct(float $side)
    {
        $this->side = $side;
    }

    public function area(): float
    {
        return $this->side * $this->side;
    }
}
Step 4: Implement a function to calculate total area of multiple shapes
<?php

// AreaCalculator.php - Function to calculate total area of multiple shapes
class AreaCalculator
{
    public function getTotalArea(array $shapes): float
    {
        $totalArea = 0;

        foreach ($shapes as $shape) {
            if (!$shape instanceof Shape) {
                throw new InvalidArgumentException('Invalid shape provided.');
            }

            $totalArea += $shape->area();
        }

        return $totalArea;
    }
}

In this example, we have created the Shape interface as a contract for all shapes. Both Rectangle and Square classes implement the Shape interface and provide their own implementation of the area() method, adhering to the first and second points of Liskov Substitution Principle.

The AreaCalculator class accepts an array of shapes and calculates the total area by calling the area() method on each shape object. Before calculating the area, it checks if each object is an instance of the Shape interface, fulfilling the fifth point of Liskov Substitution Principle.

This example follows PSR standards, uses interfaces to define a contract, and ensures that the child classes (Rectangle and Square) can be used interchangeably with the parent class (Shape) without introducing unexpected behavior or violating the contract established by the parent class.

Example usage:

<?php

$shapes = [
    new Rectangle(5, 3),
    new Square(4),
    new Rectangle(2, 8),
    new Square(6),
];

$areaCalculator = new AreaCalculator();
$totalArea = $areaCalculator->getTotalArea($shapes);

echo "Total Area of Shapes: " . $totalArea . PHP_EOL;

Conclusion

By following the Liskov Substitution Principle and adhering to PSR best practices, we ensure that our codebase remains flexible and maintainable. The Rectangle and Square classes in our example can be used interchangeably with the Shape interface, promoting a robust and consistent design. Understanding and applying LSP not only improves the quality of our code but also fosters a more scalable and extensible software architecture.

Me Gusta
  • Facebook
  • Twitter
  • LinkedIN
  • Pinterest
Share
Etiquetado en