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);