Home > Backend Development > PHP Problem > How to implement a single column mode in php

How to implement a single column mode in php

藏色散人
Release: 2023-03-10 14:40:02
Original
1581 people have browsed it

How to implement a single-column mode in php: 1. Create a private variable to save the object; 2. Prohibit the use of new; 3. Prohibit cloning; 4. Determine whether the object exists; 5. Implement the single-column mode.

How to implement a single column mode in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php implement a single column mode?

PHP implements a single column mode

Code:

<?php
final class Singleton
{
    //1.创建私有变量保存该对象
    private static $instance;
    //2.禁止使用new
    private function __construct(){ }
    //3.禁止克隆
    private function __clone() { }
    //4.判断对象是否存在
    public static function getInstance()
    {
        if (!self::$instanceinstanceof self) {
            self::$instance= new self();
        }
        return self::$instance;
    }
    public function test(){
        echo &#39;测试单列模式&#39;;
    }
}
$singleton = Singleton::getInstance();
$singleton->test();
//实现单列模式的意义,减少资源的占用
Copy after login

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to implement a single column mode in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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