<?php
namespace app\index\controller;
class Demo1
{
//绑定一个类到容器
public function bindClass()
{
//把一个类放到容器中:相当于注册到容器中
\think\Container::set('temp', '\app\common\Temp');
//将容器中的类实例化,并取出来用:实例化时可调用构造器进行初始化
$temp = \think\Container::get('temp', ['name' => 'Peter_Zhu']);
return $temp->getName();
}
//绑定一个闭包到容器:闭包现在理解为一个匿名函数
public function bindClose()
{
//把一个闭包放到容器中:相当于注册到容器中
\think\Container::set('demo', function (domain;
});
//将容器中的闭包取出来用
return \think\Container::get('demo', ['domain' => 'http://www.php.cn']);
}
}