singleton pattern in php

The singleton pattern allows you to instantiate an object only once

class human{

    public $bb;

    static public $gg = null;

    final public function __constuct{ //This is the final constructor, which is called directly after the class is instantiated and this constructor is not allowed to be modified

         $this->bb = mt_rand(10000,3000000);       

    }

    static public function getins(){ //This is a static function that can be called directly without instantiation. Writing method human::getins()

        if(human::$gg===null){

            human::$gg = new human(); //The value of the instantiated object is assigned to the variable $gg

        }

        return human::$gg;     

    }

}

class man extends human{

    public function __construct(){}

}

var_dump(human::getins());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324818852&siteId=291194637