Home>Article>Backend Development> Are PHP design patterns commonly used?

Are PHP design patterns commonly used?

(*-*)浩
(*-*)浩 Original
2019-09-19 09:29:09 2134browse

Design pattern

Are PHP design patterns commonly used?

##Design pattern is a It is a summary of code design experience that has been used repeatedly, is known to most people, and has been classified and cataloged. The purpose of using design patterns is to reuse code, make the code easier to understand by others, and ensure code reliability.There is no doubt that design patterns are win-win for ourselves, others, and the system; design patterns make coding truly engineering; design patterns are the cornerstone of software engineering, just like the structure of a building. (Recommended learning:PHP programming from entry to proficiency)

Why should we advocate design pattern (Design Pattern)?

The fundamental reason is to reuse code and increase maintainability.

Six principles of design patterns

Open and closed principle: A software entity such as a class, module, and function should be open to extension and closed to modification.

Richter Substitution Principle: All places that reference a base class must be able to transparently use objects of its subclasses.

Dependency Inversion Principle: High-level modules should not depend on low-level modules, both should Depend on its abstraction; abstractions should not depend on details; details should depend on abstractions.

Single Responsibility Principle: Do not have more than one reason for class changes. In layman's terms, a class is only responsible for one responsibility.

Interface isolation principle: The client should not rely on interfaces it does not need; the dependence of one class on another class should be based on the smallest interface.

Demeter's Law: An object should keep the least knowledge about other objects.

Design patterns implement these principles to achieve code reuse and increase maintainability.

Single case mode

The so-called singleton mode is to ensure that there is only one instance of a certain class, and it instantiates itself and provides this instance to the entire system, that is, in the application Only one instance of this class will exist in the program.

Usually the singleton mode is used in instances that only allow database access to objects, thereby preventing multiple database connections from being opened. The singleton mode is a common design pattern. In computer systems, thread pools, caches, log objects, Dialog boxes, printers, database operations, and graphics card drivers are often designed as singletons.

The singleton mode has the following three characteristics:

1. There can only be one instance, it must have a constructor, and it must be marked private

2. You must create this instance yourself and have a static member variable that saves the instance of the class

3. This instance must be provided to other objects, and there must be a public static method to access this instance

The singleton class cannot be instantiated directly in other classes, but can only be instantiated by itself. It does not create a copy of the instance, but returns a reference to the instance stored inside the singleton class

So why use the PHP singleton pattern?

One of the main application scenarios of PHP is the scenario where the application deals with the database. In an application, there will be a large number of database operations. For the behavior of connecting the database handle to the database, using the singleton mode can avoid a large number of operations. new operation. Because every new operation consumes system and memory resources.

The above is the detailed content of Are PHP design patterns commonly used?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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