Home Backend Development PHP Tutorial A brief discussion on the understanding of PHP singleton mode and sample code

A brief discussion on the understanding of PHP singleton mode and sample code

Apr 19, 2019 pm 04:05 PM
php Singleton pattern

This article is about understanding the PHP singleton mode. Friends who are interested should learn more!

Why use singleton mode?
I checked the information online and recorded it so that I can check it later.
Singleton mode, as the name suggests, has only one instance. It can save memory and resources, mainly because when PHP is dealing with the database, every new object will consume a certain amount of resources.

As we all know, the PHP language is an interpreted scripting language. This operating mechanism allows all related resources to be recycled after each PHP page is interpreted and executed. In other words, PHP has no way to make an object resident in memory at the language level. This is different from compiled types such as asp.net and Java. For example, in Java, a singleton will always exist throughout the life cycle of the application. Variables are cross-page level

, which can truly make this instance unique in the application life cycle. However, in PHP, all variables, whether they are global variables or static members of the class, are page-level. Every time the page is executed, a new object will be re-established and will be cleared after the page is executed. It seems that PHP The singleton mode is meaningless, so I think the PHP singleton mode is very meaningful only when multiple application scenarios occur during a single page

level request and need to share the same object resource.

Without further ado, let’s start with the code

<span style="font-size: 14px;"><span style="font-size: 12px;"><?php</span><br/><br/><span style="font-size: 12px;">class User</span><br/><span style="font-size: 12px;">{</span><br/><span style="font-size: 12px;">    /*</span><br/><span style="font-size: 12px;">     * 1、创建一个存放对象的私有化静态变量</span><br/><span style="font-size: 12px;">     * 2、私有化克隆方法</span><br/><span style="font-size: 12px;">     * 3、私有化构造方法</span><br/><span style="font-size: 12px;">     * 4、创建实例化对象的唯一入口</span><br/><span style="font-size: 12px;">     *</span><br/><span style="font-size: 12px;">     * **/</span><br/><span style="font-size: 12px;">    private static $_instance = &#39;&#39;;</span><br/><span style="font-size: 12px;">    private function __clone(){}</span><br/><span style="font-size: 12px;">    private function __construct(){}</span><br/><span style="font-size: 12px;">    static public function getInstance()</span><br/><span style="font-size: 12px;">    {</span><br/><span style="font-size: 12px;">        if(is_null(self::$_instance) || isset(self::$_instance)){</span><br/><span style="font-size: 12px;">            self::$_instance = new User();</span><br/><span style="font-size: 12px;">        }</span><br/><span style="font-size: 12px;">        return self::$_instance;</span><br/><span style="font-size: 12px;">    }</span><br/><span style="font-size: 12px;">    public function getIp()</span><br/><span style="font-size: 12px;">    {</span><br/><span style="font-size: 12px;">        return $_SERVER[&#39;SERVER_ADDR&#39;];</span><br/><span style="font-size: 12px;">    }</span><br/><span style="font-size: 12px;">}</span><br/><span style="font-size: 12px;">$op =  User::getInstance();</span><br/><span style="font-size: 12px;">echo $op->getIp();</span><br/><br/></span>
Copy after login

Related tutorials: PHP video tutorial

The above is the detailed content of A brief discussion on the understanding of PHP singleton mode and sample code. For more information, please follow other related articles on the PHP Chinese website!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles