PHP自己的框架2.0设置常量并绑定容器(重构篇三)

目录

1、设置常量并绑定容器

2、容器增加设置当前容器的实例和绑定一个类实例当容器

3、将常量绑定到容器中

4、运行效果


1、设置常量并绑定容器

2、容器增加设置当前容器的实例和绑定一个类实例当容器
 //设置当前容器的实例

    public static function setInstance($instance)
    {
        static::$instance = $instance;
    }


    //绑定一个类实例当容器

    public function bandInstance($abstract, $instance)
    {
        if ($instance instanceof \Closure) {
            $this->bind[$abstract] = $instance;
        } else {
            if (isset($this->bind[$abstract])) {
                $abstract = $this->bind[$abstract];
            }

            $this->instances[$abstract] = $instance;
        }

        return $this;
    }
3、将常量绑定到容器中
<?php
namespace fm;
class core extends Di{
    protected $corePath;//核心目录路径
    protected $rootPath;//项目根目录
    public function __construct($appPath = '')
    {

    }
    public  function run(){
        $this->_int();//初始化常量
        echo '框架运行中';
    }
     public function _int(){

         //获取框架核心路径 都替换/以便兼容linux
         $path=str_replace("\\","/",__FILE__);
         //定义常量
         $this->corePath=dirname(dirname($path));
         $this->rootPath=dirname($this->corePath);
         static::setInstance($this);
         $this->bandInstance('core', $this);
         var_dump( $this->getInstance());
         exit;
     }

}
4、运行效果

猜你喜欢

转载自blog.csdn.net/weixin_39934453/article/details/132858653