Home  >  Article  >  Backend Development  >  Detailed explanation of php registration tree

Detailed explanation of php registration tree

无忌哥哥
无忌哥哥Original
2018-06-28 16:15:102281browse

* Registration tree: In fact, it is to create an object set, also called an object pool, which is stored in an array.

//Declare three classes first, and then throw them into the object tree

class Demo1 {}
class Demo2 {}
class Demo3 {}

//Declare the object registration tree class

class Register
{
    //静态属性中保存着所有已经挂载到树上的对象
    public static $objs = [];
    
    //将对象挂载到树上
    public static function set($index,$obj)
    {
        self::$objs[$index] = $obj;
    }
    
    //取出对象使用
    public static function get($index)
    {
        return self::$objs[$index];
    }
    
    //已经无效的对象,及时销毁,节省资源
    public static function del($index)
    {
        unset(self::$objs[$index]);
    }
}

//First instantiate the three classes and then mount them on the object tree

Register::set('demo1',new Demo1);
Register::set('demo2',new Demo2);
Register::set('demo3',new Demo3);

//Check whether the tree is on the tree?

var_dump(Register::$objs);
echo &#39;<hr>&#39;;
echo &#39;<pre class="brush:php;toolbar:false">&#39;.print_r(Register::$objs,true).&#39;
'; echo '
';

//Use the get method of the registered class to view

var_dump(Register::get(&#39;demo2&#39;));

//Delete an instance object in the object pool

Register::del(&#39;demo2&#39;);

//View the demo2 object again, it can no longer be Checked it because it was destroyed

var_dump(Register::get(&#39;demo2&#39;));

The above is the detailed content of Detailed explanation of php registration tree. For more information, please follow other related articles on the PHP Chinese website!

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