Three commonly used design patterns in PHP_php skills

高洛峰
Release: 2023-03-05 21:14:01
Original
1282 people have browsed it

This article introduces three commonly used PHP design patterns: singleton mode, factory mode, and observer mode. It has a good reference value. Let’s take a look at it with the editor.

1. First of all, let’s look at the singleton mode

The so-called singleton mode means that there will only be one instance of this class in the application.

Usually singleton mode is used in instances that only allow database access to objects, thus preventing multiple database connections from being opened.

A singleton class should include the following points:

Unlike ordinary classes, singleton classes cannot be instantiated directly, only is instantiated by itself. Therefore, to obtain such restrictive effects, the constructor must be marked private.

In order for a singleton class to function without being instantiated directly, such an instance must be provided for it. Therefore, it is necessary for the singleton class to have a private static member variable that can save the instance of the class and a corresponding public static method that can access the instance.

In PHP, in order to prevent the cloning of the singleton class object from breaking the above implementation form of the singleton class, an empty private __clone() method is usually provided for the base.

The singleton pattern ensures that a class has only one instance, instantiates itself and provides this instance to the entire system.

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.

There are three types of singleton modes: lazy-style singleton, hungry-style singleton, and registration-style singleton.

The singleton mode has the following three characteristics:

1. There can only be one instance.

2. You must create this instance yourself.

3. This instance must be provided to other objects.

So why use PHP singleton mode?

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.

For more articles related to the three commonly used design patterns in PHP, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template