Home > Article > Backend Development > Detailed explanation of php registration tree
* 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 '<hr>'; echo '<pre class="brush:php;toolbar:false">'.print_r(Register::$objs,true).''; echo '
//Use the get method of the registered class to view
var_dump(Register::get('demo2'));
//Delete an instance object in the object pool
Register::del('demo2');
//View the demo2 object again, it can no longer be Checked it because it was destroyed
var_dump(Register::get('demo2'));
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!