Home Backend Development PHP Tutorial Detailed explanation of the five object-oriented principles of PHP and the single responsibility principle

Detailed explanation of the five object-oriented principles of PHP and the single responsibility principle

Apr 11, 2018 am 10:52 AM
php

The examples in this article describe the Single Responsibility Principle (SRP) of the five object-oriented principles of PHP. Share it with everyone for your reference. The details are as follows:

Single Pesponsibility Principle (SRP)

Single responsibility has two meanings: One is to avoid the same Responsibilities are dispersed into different classes. The other is to avoid one class taking on too many responsibilities

Why should we comply with SRP?

(1) It can reduce the coupling between classes

If you reduce the coupling between classes, when the requirements change, only one class is modified, thus isolating the change; if a class has multiple different responsibilities, they are coupled together, and when one responsibility changes, it may Interfere with other responsibilities.

(2) Improve the reusability of classes

It is much easier to modify a computer than to repair a TV. The main reason is that the coupling between the various components of the TV is too high, but it is different from the computer. The computer's memory, hard disk, sound card, network card, keyboard light and other components can be easily disassembled and assembled separately. If a part is broken, just replace it with a new one. The above example demonstrates the advantages of single responsibility. Due to the use of single responsibility, 'components' can be easily 'disassembled' and 'assembled'.

Failure to comply with SRP will affect the reusability of classes. When you only need to use a certain responsibility of the class, it is difficult to separate because it is coupled with other responsibilities.

Does complying with SRP have any application in actual code development? some. Take the data persistence layer as an example. The so-called data persistence layer mainly refers to database operations, and of course, cache management. At this time, the data persistence layer needs to support multiple databases. What should be done? Define multiple database operation classes? The idea is already very close. The next step is to use the factory pattern.

Factory pattern (Faction) allows you to instantiate objects when the code is executed. It is called Factory Pattern because it is responsible for ‘producing objects’. Taking the database as an example, what the factory needs is to generate different instantiated objects based on different parameters. The simplest factory is to instantiate an object based on the type name passed in. If it is passed in to MySQL, it calls the MySQL class and instantiates it. If it is SQLite, it calls the SQLite class and instantiates it. It can even handle TXT, Execl, etc.' class database'.

The factory class is such a class, it is only responsible for producing objects, but not the specific content of the objects.

The following is an example

Define an adapter interface

  1. interface Db_Adpater
    {
      /**
       * 数据库连接
       * @param $config 数据库配置
       * @return mixed resource
       */
      public function connect($config);
      /**
       * 执行数据库查询
       * @param $query 数据库查询的SQL字符串
       * @param $handle 连接对象
       * @return mixed
       */
      public function query($query,$handle);
    }
    Copy after login

Define a MySQL database operation class that implements the DB_Adpater interface

  1. class Db_Adapter_Mysql implements Db_Adpater
    {
      private $_dbLink;  //数据库连接字符串标识
      /**
       * 数据库连接函数
       * @param $config 数据库配置
       * @return resource
       * @throws Db_Exception
       */
      public function connect($config)
      {
        if($this->_dbLink = @mysql_connect($config->host . (empty($config->port) ? '' : ':' . $config->prot) ,$config->user, $config->password, true))
        {
          if(@mysql_select_db($config->database, $this->_dbLink))
          {
            if($config->charset)
            {
              mysql_query("SET NAME '{$config->charset}'", $this->_dbLink);
            }
            return $this->_dbLink;
          }
        }
        throw new Db_Exception(@mysql_error($this->_dbLink));
      }
      /**
       * 执行数据库查询
       * @param $query 数据库查询SQL字符串
       * @param $handle 连接对象
       * @return resource
       */
      public function query($query,$handle)
      {
        if($resource = @mysql_query($query,$handle))
          return $resource;
      }
    }
    Copy after login

Define a SQLite database operation class that implements the DB_Adpater interface

  1. class Db_Adapter_sqlite implements Db_Adpater
    {
      private $_dbLink;  //数据库连接字符串标识
      public function connect($config)
      {
        if($this->_dbLink = sqlite_open($config->file, 0666, $error))
        {
          return $this->_dbLink;
        }
        throw new Db_Exception($error);
      }
      public function query($query, $handle)
      {
        if($resource = @sqlite_query($query,$handle))
        {
          return $resource;
        }
      }
    }
    Copy after login

Now If you need a database operation method, you only need to define a factory class and generate the required classes according to the different inputs.

  1. class sqlFactory
    {
      public static function factory($type)
      {
        if(include_once 'Drivers/' . $type . '.php')
        {
          $classname = 'Db_Adapter_'.$type;
          return new $classname;
        }
        else
          throw new Exception('Driver not found');
      }
    }
    Copy after login

When called, Just write like this

  1. $db = sqlFactory::factory('MySQL');
    $db = sqlFactory::factory('SQLite');
    Copy after login

我们把创建数据库连接这块程序单独拿出来,程序中的CURD就不用关心什么数据库了,只要按照规范使用对应的方法即可。

工厂方法让具体的对象解脱出来,使其不再依赖具体的类,而是抽象。

设计模式里面的命令模式也是SRP的体现,命令模式分离“命令的请求者”和“命令的实现者”方面的职责。举一个很好理解的例子,就是你去餐馆订餐吃饭,餐馆存在顾客、服务员、厨师三个角色。作为顾客,你要列出菜单,传给服务员,由服务员通知厨师去实现。作为服务员,只需要调用准备饭菜这个方法(对厨师喊“该炒菜了”),厨师听到要炒菜的请求,就立即去做饭。在这里,命令的请求和实现就完成了解耦。

模拟这个过程,首先定义厨师角色,厨师进行实际做饭、烧汤的工作。

以下是示例

  1. /**
     * 厨师类,命令接受者与执行者
     * Class cook
     */
    class cook
    {
      public function meal()
      {
        echo '番茄炒鸡蛋',PHP_EOL;
      }
      public function drink()
      {
        echo '紫菜蛋花汤',PHP_EOL;
      }
      public function ok()
      {
        echo '完毕',PHP_EOL;
      }
    }
    //然后是命令接口
    interface Command
    {
      public function execute();
    }
    Copy after login

轮到服务员出场,服务员是命令的传送者,通常你到饭馆吃饭都是叫服务员吧,不能直接叫厨师,一般都是叫“服务员,给我来盘番茄炒西红柿”。所以,服务员是顾客和厨师之间的命令沟通都。

  1. class MealCommand implements Command
    {
      private $cook;
      //绑定命令接受者
      public function __construct(cook $cook)
      {
        $this->cook = $cook;
      }
      public function execute()
      {
        $this->cook->meal();//把消息传给厨师,让厨师做饭,下同
      }
    }
    class DrinkCommand implements Command
    {
      private $cook;
      //绑定命令接受者
      public function __construct(cook $cook)
      {
        $this->cook = $cook;
      }
      public function execute()
      {
        $this->cook->drink();
      }
    }
    Copy after login

现在顾客可以按照菜单叫服务员了

  1. class cookControl
    {
      private $mealcommand;
      private $drinkcommand;
      //将命令发送者绑定以命令接收器上面来
      public function addCommand(Command $mealcommand, Command $drinkcommand)
      {
        $this->mealcommand = $mealcommand;
        $this->drinkcommand = $drinkcommand;
      }
      public function callmeal()
      {
        $this->mealcommand->execute();
      }
      public function calldrink()
      {
        $this->drinkcommand->execute();
      }
    }
    Copy after login

好了,现在完成整个过程

  1. $control = new cookControl;
    $cook = new cook;
    $mealcommand = new MealCommand($cook);
    $drinkcommand = new DrinkCommand($cook);
    $control->addCommand($mealcommand,$drinkcommand);
    $control->callmeal();
    $control->calldrink();
    Copy after login

从上面的例子可以看出,原来设计模式并非纯理论的东西,而是来源于实际生活,就连普通的餐馆老板都懂设计模式这门看似高深的学问。其实,在经济和管理活动中对流程的优化就是对各种设计模式的摸索和实践。所以,设计模式并非计算机编程中的专利。事实上,设计模式的起源并不是计算机,而是源于建筑学。

在设计模式方面,不仅以上这两种体现了SRP,还有别的(比如代理模式)也体现了SRP。SRP不只是对类设计有意义,对以模块、子系统为单位的系统架构设计同样有意义。

模块、子系统也应该仅有一个引起它变化的原因,如MVC所倡导的各个层之间的相互分离就是SRP在系统总体设计中的应用。

SRP是最简单的原则之一,也是最难做好的原则之一。我们会很自然地将职责连接在一起。找到并且分离这些职责是软件设计需要达到的目的

一些简单的应用遵循的做法如下:

根据业务流程,把业务对象提炼出来。如果业务的流程的链路太复杂,就把这个业务对象分离为多个单一业务对象。当业务链标准化后,对业务对象的内部情况做进一步处理,把第一次标准化视为最高层抽象,第二次视为次高层抽象,以此类推,直到“恰如其分”的设计层次

职责的分类需要注意。有业务职责,还要有脱离业务的抽象职责,从认识业务到抽象算法是一个层层递进的过程。就好比命令模式中的顾客,服务员和厨师的职责,作为老板(即设计师)的你需要规划好各自的职责范围,即要防止越俎代庖,也要防止互相推诿。

The above is the detailed content of Detailed explanation of the five object-oriented principles of PHP and the single responsibility principle. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

See all articles