<?php
namespace app\index\controller;
class Demo1
{
//绑定一个类到容器
public function bindClass()
{
//把一个类放到容器中:相当与注册到容器中
\think\Container::set('temp','\app\common\Temp');
//使用助手函数 bind 建议不要使用助手函数
// bind('temp','\app\common\Temp');
//将容器中的类实例化并取出来用:实例化同时调用构造器进行初始化
$temp = \think\Container::get('temp',['name'=>'Peter_Zhu']);
//使用助手函数 app
// $temp = app('temp',['name'=>'PHP中文网']);
return $temp->getName();
}
}
<?php
namespace app\common;
class Temp
{
public function __construct($name="Peter")
{
$this->name = $name;
}
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return '方法是:'.__METHOD__.'属性是:'.$this->name;
}
}