SOLID principles PHP Edition. Today: Single Responsibility Principle

Jose Cerrejon
3 min readApr 9, 2024

--

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 Principle: A class should be open for extension but closed for modification.
· Liskov Substitution Principle: 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 of 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();
$order->addItem($item1);
$order->addItem($item2);

$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 calculating the total, and handling 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. 🙂

This article was published for the first time on the blog misapuntesde.com, where you can read news/tutorials about Raspberry Pi & Linux, and soon about Mac, DevOps, development, and all the things you and I want to learn.

I’m looking for a job, so visit my LinkedIn profile here! Thank you!

--

--