Six Principles of PHP design

Source: Internet
Author: User

1. Single Responsibility

Definition: Do not have more than one cause for class changes. In layman's terms, a class is responsible for only one responsibility.

Advantages:

1), can reduce the complexity of the class, a class is only responsible for a responsibility, logic simple;

2), improve the readability of the class, improve the maintainability of the system;

3), the risk of change caused by the reduction, change is inevitable.

2. The principle of substitution on the Richter scale

Definition: All references to the base class must be able to use the object of its subclass transparently, that is, the subclass can extend the functionality of the parent class, but not the original function of the parent class

3. Dependency Inversion principle

Definition: High-level modules should not be dependent on the lower layers, both should rely on their abstraction; abstractions should not depend on detail; detail should be dependent on abstraction.

This is the most difficult to understand, usually in the framework of the project to use, for example, the business logic layer is a high-level module relative to the data layer, because the business logic layer needs to call the data layer to connect to the database, but to achieve scalable high-reuse, try not to let the business logic layer depends on the data layer, An interface can be abstracted from the data layer, allowing the business logic layer to rely on this abstract interface.

Advantages:

1), low-level modules as far as possible to have an abstract class or interface, or both.

2), the declaration type of the variable as far as possible is abstract class or interface.

3), the use of inheritance to follow the Richter replacement principle.

4. Interface Isolation principle

Definition: The client should not rely on interfaces it does not need, and the dependency of one class on another should be based on the smallest interface.

Attention:

1), the interface as small as possible, but there must be a limit. The refinement of the interface can improve the design flexibility is not a fact, but if too small, it will result in an excessive number of interfaces, resulting in complex designs. So be sure to be modest.

2), to customize the service for the interface-dependent class, exposes only to the calling class the method it requires, and the method it does not need is hidden. A minimal dependency can be established only by focusing on providing a customized service for a module.

3), improve cohesion, reduce external interaction. Enable the interface to do the most things with the least amount of method.

5. Dimitri rule (least known principle)

Definition: An object should have minimal knowledge of other objects.

The simple understanding is that cohesion, a class of methods and properties can be privatized as far as possible with private.

Attention:

1), only communicate with the direct friends, do not talk with strangers.

2), excessive use of the principle, will lead to greater complexity of the system. Therefore, in the adoption of Dimitri Law, we should weigh repeatedly, not only to achieve a clear structure, but also high cohesion and low coupling.

6. Opening and closing principle

Definition: A software entity such as classes, modules, and functions should be open to extensions and closed for modification.

Scenario: During the software lifecycle, because changes, upgrades, and maintenance are required to modify the software's original code, errors may be introduced into the old code, or we will have to refactor the entire functionality and require the original code to be re-tested.

Recommendation: When software requirements change, try to implement changes by extending the behavior of the software entities, rather than by modifying existing code.

Six Principles of PHP design

Related Article

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.