返回依赖注入案例...登陆

依赖注入案例

昕旸2019-04-10 12:40:57207
class Father{
    public function output(){
    return "hello world";
    }
    }

//使用构造方法实现依赖注入
class Son{
    private $father = null;//制造容器来储存
    public function __construct(Father $father)
    {
        $this -> father = $father;//把大类传入到小类中保存起来了
        }
    public function getInfo()
    {
        return $this->father->output();
        }
        
        
 //在函数中进行使用
 $father = new Father;
 $son = new Son($father);
 
 echo $son->output();


最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送