php register mode factory mode

<? php 
/ ** 
 * Registrar mode 
 * global shared and exchanged objects 
 * / 

class Register 
{ 
    public static $ objects; // define global array 

    // save objects to global array 
    public static function set ($ name, $ object) 
    { 
        self :: $ objects [$ name] = $ object; 
    } 

    // Get an object 
    public static function get ($ name) 
    { 
        if (! isset (self :: $ objects [$ name])) { 
            return false; 
        } 
        return self :: $ objects [$ name]; 
    } 

    // Get all objects 
    public static function getAll () 
    { 
        return self :: $ objects; 
    } 

    // Delete the specified object
    public function remove($name)
    {
        unset(self::$objects[$name]);
    }
}

class Db
{
    public function say()
    {
        echo 444444444444;
    }
}


class Factory
{
    public static function getDb()
    {
        $db = new Db();
        Register::set('db',$db);
        return $db;
    }
}

Factory::getDb();
$db = Register::get('db');
var_dump($db->say());

 

https://zhuanlan.zhihu.com/p/110541976

https://cloud.tencent.com/developer/article/1556340

https://www.cnblogs.com/rxbook/p/10452582.html

Guess you like

Origin www.cnblogs.com/php-linux/p/12717520.html