Home Backend Development PHP Tutorial php设计模式之单例模式使用示例_php实例

php设计模式之单例模式使用示例_php实例

May 17, 2016 am 08:50 AM
php design patterns Singleton pattern

以下为单例模式代码:

复制代码 代码如下:

class EasyFramework_Easy_Mysql{
    protected static $_instance = null;
    private function __construct(){

    }
    public static function getInstance(){
        if (self::$_instance === null){
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    protected function __clone(){

    }

}

$x = EasyFramework_Easy_Mysql::getInstance();

var_dump($x);


?>


/*
 * 1.第一步:
 * 既然是单例,也就是只能实例化一次,也就代表在实例化时
 * 不可能使用new关键字!!!!
 * 在使用new关键字时,类中的构造函数将自动调用。
 * 但是,如果我们将构造函数的访问控制符设置为protected或private
 * 那么就不可能直接使用new关键字了!!!
 * 第二步:
 * 无论protected/private修饰的属性或方法,请问在当前类的
 * 内部是否可以访问?---> 可以
 * 第三步:
 * 现在我们根本没有办法得到对象(因为你不能使用new关键字了),
 * 第四步:静态成员(包括属性或方法)在访问时,只能通过
 * 类名称::属性()
 * 类名称::方法()
 * 第五步:如果我现在存在一个静态方法--> getInstance()
 * 那么在调用时就应写成
 * $object = EasyFramework_Easy_Mysql::getInstance()
 * 如果,getInstance()方法可以得到唯一的一个对象
 * 也就代表是所谓的单例模式了!!!
 * 第六步,怎么让getInstace()只得到一个对象呢?
 * 既然要得到对象,那么肯定是:
 * $variabl = new ????();
 * 我们又知道静态属性的值是可以所有的对象来继承的!!!
 * 静态成员是属于类的,而非对象的!
 * 所以:
 * 第七步:声明一个静态的属性,用其存储实例化的对象
 * protectd static $_instance
 *
 * 并且初始值为null
 * 那么我在调用getInstance()方法时,只需要判断其值是否为空即可
 *
 * public static function getInstance(){
 *     if(self::_instance === null){
 *      self::_instance = new self();
 *  }
 *  return self::_instance;
 * }
 * 在实例时,一定是这样写:
 * $x = EasyFramework_Easy_Mysql::getInstance();
 * 在第一时调用时,类的$_instance这个静态属性值为null,
 * 那么也就代表,getInstance()方法的判断条件为真了,
 * 也就意味着
 * self::$_instance这个成员有了值了!!!
 * 并且还返回这个值
 * $y = EasyFramework_Easy_Mysql::getInstance();
 * 在第二次或第N次调用时,self::$_instance已经有了值了
 * 也就代表getInstance()方法的条件为假了!!!
 * 也就代表其中的程序代表不可能执行了!!!
 * 也就代表将直接返回以前的值了!!!
 *
 *
 *
 * */

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

One article to understand the singleton pattern in JavaScript One article to understand the singleton pattern in JavaScript Apr 25, 2023 pm 07:53 PM

The JS singleton pattern is a commonly used design pattern that ensures that a class has only one instance. This mode is mainly used to manage global variables to avoid naming conflicts and repeated loading. It can also reduce memory usage and improve code maintainability and scalability.

The application of singleton mode and factory mode in C++ function overloading and rewriting The application of singleton mode and factory mode in C++ function overloading and rewriting Apr 19, 2024 pm 05:06 PM

Singleton pattern: Provide singleton instances with different parameters through function overloading. Factory pattern: Create different types of objects through function rewriting to decouple the creation process from specific product classes.

Getting Started with PHP: Singleton Pattern Getting Started with PHP: Singleton Pattern May 20, 2023 am 08:13 AM

In software development, we often encounter situations where multiple objects need to access the same resource. In order to avoid resource conflicts and improve program efficiency, we can use design patterns. Among them, the singleton pattern is a commonly used way to create objects, which ensures that a class has only one instance and provides global access. This article will introduce how to use PHP to implement the singleton pattern and provide some best practice suggestions. 1. What is the singleton mode? The singleton mode is a commonly used way to create objects. Its characteristic is to ensure that a class has only one instance and provides

Commonly used design patterns in PHP and their implementation methods Commonly used design patterns in PHP and their implementation methods Jun 27, 2023 pm 01:08 PM

PHP is a widely used and very popular programming language. PHP is a very important part of today's web applications. Design patterns play a vital role in developing PHP applications. Design pattern is a template for solving problems that can be reused in different environments. It helps us write better code and make the code more reliable, maintainable and scalable. In this article, we will explore some commonly used design patterns in PHP and how to implement them. Singleton Pattern Singleton pattern is a creation pattern that allows

PHP Design Patterns: The Path to Code Excellence PHP Design Patterns: The Path to Code Excellence Feb 21, 2024 pm 05:30 PM

Introduction PHP design patterns are a set of proven solutions to common challenges in software development. By following these patterns, developers can create elegant, robust, and maintainable code. They help developers follow SOLID principles (single responsibility, open-closed, Liskov replacement, interface isolation and dependency inversion), thereby improving code readability, maintainability and scalability. Types of Design Patterns There are many different design patterns, each with its own unique purpose and advantages. Here are some of the most commonly used PHP design patterns: Singleton pattern: Ensures that a class has only one instance and provides a way to access this instance globally. Factory Pattern: Creates an object without specifying its exact class. It allows developers to conditionally

In PHP, what is the concept of singleton design pattern? In PHP, what is the concept of singleton design pattern? Aug 18, 2023 pm 02:25 PM

The Singleton pattern ensures that a class has only one instance and provides a global access point. It ensures that only one object is available and under control in the application. The Singleton pattern provides a way to access its unique object directly without instantiating the object of the class. Example<?php classdatabase{ publicstatic$connection; privatefunc

Thoughts on thread safety issues in singleton mode in PHP Thoughts on thread safety issues in singleton mode in PHP Oct 15, 2023 am 10:14 AM

Thinking about thread safety issues of singleton mode in PHP In PHP programming, singleton mode is a commonly used design pattern. It can ensure that a class has only one instance and provide a global access point to access this instance. However, when using the singleton pattern in a multi-threaded environment, thread safety issues need to be considered. The most basic implementation of the singleton pattern includes a private constructor, a private static variable, and a public static method. The specific code is as follows: classSingleton{pr

Extension and customization of singleton pattern in PHP framework Extension and customization of singleton pattern in PHP framework Oct 15, 2023 am 11:10 AM

Extension and customization of singleton mode in PHP framework [Introduction] Singleton mode is a common design pattern, which ensures that a class can only be instantiated once in the entire application. In PHP development, the singleton pattern is widely used, especially in the development and expansion of frameworks. This article will introduce how to extend and customize the singleton pattern in the PHP framework and provide specific code examples. [What is the singleton pattern] The singleton pattern means that a class can only have one object instance and provides a global access point for external use. In PHP development, pass

See all articles