Skip to main content

SOLID principles PHP Edition. Today, Single Responsibility Principle

Jose CerrejonAbout 2 minDeveloper

SOLID principles PHP Edition. Today: Single Responsibility Principle

Single Responsibility Principle
Single Responsibility Principle. Generated with AI.

In the noble art of coding, you should remember the SOLID principles always. Here is a brief explanation of each principle:

  • Single Responsibility Principle: A class should have only one reason to change.
  • Open/Closed Principleopen in new window: A class should be open for extension but closed for modification.
  • Liskov Substitution Principleopen in new window: You should be able to use any subclass in place of its parent class.
  • Interface Segregation Principle: A class should not be forced to implement an interface it doesn't use.
  • Dependency Inversion Principle: High-level modules should not depend on low-level modules. Both should depend on abstractions.

Today we are going to focus on the Single Responsibility Principle.

Here you have an example on PHP about it:

class Order
    private $items = [];

    public function addItem($item)
        $this->items[] = $item;

    public function calculateTotal()
        $total = 0;
        foreach ($this->items as $item) {
            $total += $item->getPrice();
        return $total;
class Item
    private $name;
    private $price;

    public function __construct($name, $price)
        $this->name = $name;
        $this->price = $price;

    public function getPrice()
        return $this->price;
$item1 = new Item('Taco', 2.99);
$item2 = new Item('Burrito', 4.99);

$order = new Order();

$total = $order->calculateTotal();
echo "Total: $" . $total; // Total: $7.98

The Single Responsibility Principle (SRP) states that a class must have only one reason to change. This means that a class must have only one responsibility and must have no more than one reason to be modified.

In the code above, we can identify two classes: Order and Item. These classes follow the principle of single responsibility, as each has a single responsibility and has no more than one reason to change.

The Order class is responsible for representing an order and performing operations related to it, such as adding elements to the order and calculating the total, and handle the logic of the order.

The Item class represents an item and provides methods for obtaining its price. It has the responsibility to represent an article and provide information about it.

As we can see, each class has a unique responsibility and there is no mix of features in any of them.

This is beneficial because if in the future we need to make changes in the logic of the order, we just need to modify the Order class. Similarly, if we need to make changes in the representation or behavior of an article, we just need to modify the Item class. This facilitates code maintenance and reduces the risk of errors when making changes.

I hope this example helps you to understand the principle of single responsibility and how to apply it in your code.