Home Backend Development PHP Tutorial Make good use of the new features of PHP5.3 to implement singleton mode_PHP tutorial

Make good use of the new features of PHP5.3 to implement singleton mode_PHP tutorial

Jul 13, 2016 am 10:41 AM
3 php5 use new features

It can also be implemented before 5.3, but the code is more cumbersome, as follows:

class MOrder extends SModel{
protected static $handle; //single case handle

private function __construct(){
//something
}

/**
* Get the method of this type of singleton, public
*
* @return MOrder
*/
public static function instance() {
if(self::$handle){
return self::$handle;
}

$class = __CLASS__;
self::$handle = new $class();
return self::$handle;
}

//otherthing

}


5.3 Add delayed static binding (this word is really awkward)

The code is implemented as follows

class SModel {
/**
* Get the singleton handle and return the instance object of the specific model class
*/
protected static function instance() {
if(static::$handle){
return static::$handle;
}

$class = get_called_class();
static::$handle = new $class();
return static::$handle;
}

//Parent class something

}


class MGoods extends SModel{
/**
* Get the method of this type of singleton, public
* @return MGoods
*/
public static function instance(){
return parent::instance();
}
protected static $handle; //single case handle
protected function __construct(){
//something
}

//otherthing

}


Through modification, part of the implementation code of the subclass is reduced and implemented by the parent class

To be honest, it’s still very troublesome. It would be nice if PHP implemented singleton itself.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/678027.htmlTechArticleIt can also be implemented before 5.3, but the code is more cumbersome, as follows: class MOrder extends SModel{ protected static $handle; / /Single case handle private function __construct(){ //something } /** * Get this class...
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 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)

What is the difference between php5 and php8 What is the difference between php5 and php8 Sep 25, 2023 pm 01:34 PM

The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

How to use GitLab for project document management How to use GitLab for project document management Oct 20, 2023 am 10:40 AM

How to use GitLab for project document management 1. Background introduction In the software development process, project documents are very important information. They can not only help the development team understand the needs and design of the project, but also provide reference to the testing team and customers. In order to facilitate version control and team collaboration of project documents, we can use GitLab for project document management. GitLab is a version control system based on Git. In addition to supporting code management, it can also manage project documents. 2. GitLab environment setup First, I

PHP 8.3 released: new features at a glance PHP 8.3 released: new features at a glance Nov 27, 2023 pm 12:52 PM

PHP8.3 released: Overview of new features As technology continues to develop and needs change, programming languages ​​are constantly updated and improved. As a scripting language widely used in web development, PHP has been constantly improving to provide developers with more powerful and efficient tools. The recently released PHP 8.3 version brings many long-awaited new features and improvements. Let’s take a look at an overview of these new features. Initialization of non-null properties In past versions of PHP, if a class property was not explicitly assigned a value, its value

How to change port 80 in php5 How to change port 80 in php5 Jul 24, 2023 pm 04:57 PM

How to change port 80 in php5: 1. Edit the port number in the Apache server configuration file; 2. Edit the PHP configuration file to ensure that PHP works on the new port; 3. Restart the Apache server, and the PHP application will start running on the new port. run on the port.

What does TikTok recommended video mean? How to use Douyin to recommend videos? What does TikTok recommended video mean? How to use Douyin to recommend videos? Mar 27, 2024 pm 03:01 PM

As a world-renowned short video social platform, Douyin has won the favor of a large number of users with its unique personalized recommendation algorithm. This article will delve into the value and principles of Douyin video recommendation to help readers better understand and make full use of this feature. 1. What is Douyin recommended video? Douyin recommended video uses intelligent recommendation algorithms to filter and push personalized video content to users based on their interests and behavioral habits. The Douyin platform analyzes users' viewing history, like and comment behavior, sharing records and other data to select and recommend videos that best suit users' tastes from a huge video library. This personalized recommendation system not only improves user experience, but also helps users discover more video content that matches their preferences, thereby enhancing user stickiness and retention rate. at this

A guide to learn the new features of PHP8 and gain an in-depth understanding of the latest technology A guide to learn the new features of PHP8 and gain an in-depth understanding of the latest technology Dec 23, 2023 pm 01:16 PM

An in-depth analysis of the new features of PHP8 to help you master the latest technology. As time goes by, the PHP programming language has been constantly evolving and improving. The recently released PHP8 version provides developers with many exciting new features and improvements, bringing more convenience and efficiency to our development work. In this article, we will analyze the new features of PHP8 in depth and provide specific code examples to help you better master these latest technologies. JIT compiler PHP8 introduces JIT (Just-In-Time) compilation

blendOS 3: Immutable distribution system is about to be released, supporting up to 9 Linux distributions blendOS 3: Immutable distribution system is about to be released, supporting up to 9 Linux distributions Jun 06, 2023 pm 05:11 PM

According to news on June 6, developer RudraSaraswat announced the immutable distribution system blendOS3 they are developing, and promised to bring new functions and features to users. The system will support up to nine Linux distributions and provide a new way to update without using a package repository. According to the editor's understanding, blendOS3 is a distribution system that integrates ArchLinux, FedoraLinux and Ubuntu. As early as blendOS2 released in April this year, the system introduced WayDroid technology, allowing users to run Android applications on the system. One of the biggest changes brought by blendOS3 is a brand new update

What are the new features of php8 What are the new features of php8 Sep 25, 2023 pm 01:34 PM

New features of php8 include JIT compiler, type deduction, named parameters, union types, properties, error handling improvements, asynchronous programming support, new standard library functions and anonymous class extensions. Detailed introduction: 1. JIT compiler, PHP8 introduces the JIT compiler, which is an important performance improvement. The JIT compiler can compile and optimize some high-frequency execution codes in real time, thereby improving the running speed; 2. Type derivation , PHP8 introduces the type inference function, allowing developers to automatically deduce the type of variables when declaring variables, etc.

See all articles