您的位置:首页 > 其它

自己写的一个简单的容器按需加载例子

2018-01-04 10:23 363 查看
<?php

class Di {
protected $providers = [];

//注册服务
public function register($id,$val) {
$this->providers[$id] = function () use ($val){
return new $val();
};
}

//get方法
public function __get($id) {
return $this->providers[$id]();
}
}

class User{
public function test() {
echo 'user:test';
}
}

class Student{
public function test() {
echo 'student:test';
}
}

$di = new Di();

$di->register('user',User::class);
$di->register('student',Student::class);

$di->user->test()."/r/n";

$di->student->test();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: