Home > Backend Development > PHP Tutorial > Detailed explanation of php registration tree

Detailed explanation of php registration tree

无忌哥哥
Release: 2023-04-01 21:40:01
Original
2311 people have browsed it

* 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 {}
Copy after login

//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]);
    }
}
Copy after login

//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);
Copy after login

//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 '
';
Copy after login

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

var_dump(Register::get(&#39;demo2&#39;));
Copy after login
Copy after login

//Delete an instance object in the object pool

Register::del(&#39;demo2&#39;);
Copy after login

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

var_dump(Register::get(&#39;demo2&#39;));
Copy after login
Copy after login

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!

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