Understanding the Single Responsibility Principle in PHP 8 with PSR

As software developers, we constantly strive to write clean, maintainable, and scalable code. One essential principle that helps us achieve this goal is the Single Responsibility Principle (SRP). The SRP is one of the five SOLID principles of object-oriented programming, and it plays a crucial role in keeping our codebase organized and easy to manage.

What is the Single Responsibility Principle?

The Single Responsibility Principle states that a class should have only one reason to change, meaning it should have only one responsibility. In other words, a class should encapsulate a single functionality or purpose, and any change to that functionality should only affect that specific class.

Applying SRP in PHP 8 with PSR

To better understand how to apply the Single Responsibility Principle in PHP 8, let’s create a simple example of a blog post system. We will design our classes to adhere to PSR-12 coding standards, which are part of the PHP-FIG (Framework Interop Group) recommendations.

1
Create the BlogPost Class
<?php

class BlogPost
{
    private string $title;
    private string $content;

    public function __construct(string $title, string $content)
    {
        $this->title = $title;
        $this->content = $content;
    }

    public function getTitle(): string
    {
        return $this->title;
    }

    public function getContent(): string
    {
        return $this->content;
    }
}

In this example, the BlogPost class is responsible for representing individual blog posts. It has two private properties for the title and content of the post. The class provides getter methods to access the title and content.

2
Create the BlogPostManager Class
<?php

class BlogPostManager
{
    private array $posts = [];

    public function addPost(BlogPost $post): void
    {
        $this->posts[] = $post;
    }

    public function getAllPosts(): array
    {
        return $this->posts;
    }
}

The BlogPostManager class is responsible for managing blog posts. It has an array to store all the blog posts and provides methods to add a new post and retrieve all posts.

3
Usage of SRP

By separating the responsibilities of representing blog posts and managing blog posts into two different classes (BlogPost and BlogPostManager), we are adhering to the Single Responsibility Principle. The BlogPost class has the single responsibility of representing an individual post, while the BlogPostManager class has the responsibility of managing posts.

This separation of concerns makes the code more organized, maintainable, and easier to extend. If there’s a need to change the behavior related to blog posts, we know exactly where to look and modify without affecting other parts of the application.

Conclusion

The Single Responsibility Principle is a fundamental principle in object-oriented programming that encourages us to design classes with a single, well-defined responsibility. In our PHP 8 example using PSR best practices, we demonstrated how to create classes that follow the SRP, leading to clean, modular, and maintainable code.

By applying the SRP and other SOLID principles, we can build more robust and scalable applications that are easier to maintain and evolve over time.

Remember, writing good code is not just about solving immediate problems but also about ensuring that our codebase remains healthy and adaptable as our projects grow and evolve.

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