PHP object-oriented Design Principle--single responsibility principle ____php

Source: Internet
Author: User
PHP Object-oriented design principle--single responsibility principle I. Background principle of single duty (responsibility principle)There are many classes in a project, if the class does not distinguish between responsibilities, then it will appear to be very messy, the same maintenance also time-consuming; a single responsibility for a class consists of two meanings: avoiding the same responsibilities dispersed into different classes, and different responsibilities in the same class; Ii. Follow the benefits of the SRPReducing the coupling between classes to improve the reusability of classes third, the application of SRPThe principle of single responsibility is widely used in project development, the most common is the layered design used by MVC, in fact, the application of SRP in the overall design of the system;


the SRP follows the practice:Draw business objects out of business processes attention to the classification of responsibilities the idea of a single duty principle applies not only to classes, in the class method, also should have the very good embodiment, namely a method's logic cannot be too complex, but should separate the different logic, finally simplifies the method the function, enhances the code the readability; When a user submits a request and we need to do a simple validation of its parameters, the validation logic can separate several different ways to handle it individually:
Public Function Prepareexpresspush ()
{
    //get parameter 
    $info = parent::p repareexpresspush ();

    Validation parameter
    $params = [
        ' Digest ' => ' required ', '
        data ' => ' required '
    ];
    $paramResult = $this->checkparams ($info, $params);

    if (!empty ($paramResult)) {
        //Illegal request parameter, error integration processing; return
        self::errorhandler ("parameter Error");
    }

    Verify signature return
    self::checkdigest ($info);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.