search
HomeBackend DevelopmentPHP TutorialMake good use of the new features of PHP5.3 to implement singleton mode_PHP tutorial

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
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
php5和php8有什么区别php5和php8有什么区别Sep 25, 2023 pm 01:34 PM

php5和php8的区别在性能、语言结构、类型系统、错误处理、异步编程、标准库函数和安全性等方面。详细介绍:1、性能提升,PHP8相对于PHP5来说在性能方面有了巨大的提升,PHP8引入了JIT编译器,可以对一些高频执行的代码进行编译和优化,从而提高运行速度;2、语言结构改进,PHP8引入了一些新的语言结构和功能,PHP8支持命名参数,允许开发者通过参数名而不是参数顺序等等。

如何利用GitLab进行项目文档管理如何利用GitLab进行项目文档管理Oct 20, 2023 am 10:40 AM

如何利用GitLab进行项目文档管理一、背景介绍在软件开发过程中,项目文档是非常重要的资料,不仅能够帮助开发团队了解项目的需求和设计,还能提供给测试团队和客户参考。为了方便项目文档的版本控制和团队协作,我们可以利用GitLab来进行项目文档管理。GitLab是一个基于Git的版本控制系统,除了支持代码管理,还可以管理项目文档。二、GitLab环境搭建首先,我

php5如何改80端口php5如何改80端口Jul 24, 2023 pm 04:57 PM

php5改80端口的方法:1、编辑Apache服务器的配置文件中的端口号;2、辑PHP的配置文件以确保PHP在新端口上工作;3、重启Apache服务器,PHP应用程序将开始在新的端口上运行。

抖音推荐视频是什么意思?如何利用抖音推荐视频?抖音推荐视频是什么意思?如何利用抖音推荐视频?Mar 27, 2024 pm 03:01 PM

抖音作为一个全球知名的短视频社交平台,靠着其独特的个性化推荐算法赢得了广大用户的青睐。本文将深入研究抖音视频推荐的价值和原理,帮助读者更好地了解和充分利用这一功能。一、什么是抖音推荐视频抖音推荐视频是根据用户的兴趣和行为习惯,利用智能推荐算法为用户筛选和推送个性化视频内容。抖音平台通过分析用户的观看历史、点赞和评论行为、分享记录等数据,从庞大的视频库中精选出最符合用户口味的视频进行推荐。这种个性化推荐系统不仅提高了用户体验,也帮助用户发现更多符合其喜好的视频内容,从而增强用户黏性和留存率。在这个

blendOS 3:不可变发行版系统即将发布,支持多达9个Linux发行版blendOS 3:不可变发行版系统即将发布,支持多达9个Linux发行版Jun 06, 2023 pm 05:11 PM

6月6日消息,开发商RudraSaraswat宣布他们正在开发中的不可变发行版系统blendOS3,并承诺为用户带来全新的功能和特性。该系统将支持多达9个Linux发行版,并提供一种无需使用包存储库的新的更新方式。据小编了解,blendOS3是一款将ArchLinux、FedoraLinux和Ubuntu融合在一起的发行版系统。早在今年4月发布的blendOS2中,该系统引入了WayDroid技术,使得用户能够在系统上运行Android应用程序。blendOS3带来的最大变化之一是一种全新的更新

PHP8.1引入的新的Redis扩展PHP8.1引入的新的Redis扩展Jul 07, 2023 pm 09:41 PM

PHP8.1引入的新的Redis扩展随着互联网的快速发展,大量的数据需要进行存储和处理。为了提高数据处理的效率和性能,缓存成为了一个不可或缺的部分。而在PHP开发中,Redis作为一种高性能的键值对存储系统,被广泛应用于缓存和数据存储的场景。为了进一步提升Redis在PHP中的使用体验,PHP8.1引入了新的Redis扩展,本文将介绍这一扩展的新增功能,并给

CSS3的新特性一览:如何使用CSS3实现过渡效果CSS3的新特性一览:如何使用CSS3实现过渡效果Sep 09, 2023 am 11:27 AM

CSS3的新特性一览:如何使用CSS3实现过渡效果CSS3作为CSS的最新版本,在众多新特性中,最有趣和实用的应该是过渡效果(transition)。过渡效果可以让我们的页面在交互时更加平滑、漂亮,给用户带来良好的视觉体验。本文将介绍CSS3过渡效果的基本用法,并附带相应的代码示例。transition-property属性:指定需要过渡的CSS属性过渡效果

php5没有监听9000端口如何解决php5没有监听9000端口如何解决Jul 10, 2023 pm 04:01 PM

php5没有监听9000端口解决方法步骤:1、检查PHP-FPM配置文件;2、重启PHP-FPM服务;3、关闭防火墙或配置端口转发;4、检查其他进程是否占用9000端口。

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft