donglugou6652 2018-03-06 05:25
浏览 104
已采纳

在动态加载时使用类

I've been lurking into CI's Loader class and I am a bit interested about how it loads models/libraries/etc.

For example:

//Mode class person
$this->load->model('person');

and it can instantly be used:

$this->person->method();

Can somebody share some code on how to do something like this? As much as possible I don't want to use something like:

$Person = $this->mycustomloader->mycustommodel->('person);

but:

$this->mycustomloader->mycustommodel('person');
$this->person->mycustommethod();

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dqfaom241360 2018-03-06 06:00
    关注

    How its done in CI's case is that they heavily rely on passing stuff by reference (singleton), so calling $this->load->model('person') invokes the person model and assigns it back into the controller.

    You can see exactly how its done by looking at the source of the project on GitHub.

    But in a nutshell:

    <?php
    // https://github.com/bcit-ci/CodeIgniter/blob/635256265a38e655c00f4415d5f09700f28c5805/system/core/Common.php#L140
    function load_class($class) {
        return (new $class());
    }
    
    // https://github.com/bcit-ci/CodeIgniter/blob/b862664f2ce2d20382b9af5bfa4dd036f5755409/system/core/CodeIgniter.php#L316
    function &get_instance() {
        return CI_Controller::get_instance();
    }
    
    // https://github.com/bcit-ci/CodeIgniter/blob/b862664f2ce2d20382b9af5bfa4dd036f5755409/system/core/Loader.php#L359
    class CI_Loader
    {
        public function model($model, $name = '', $db_conn = FALSE)
        {
            $class = $model.'Model';
            $CI =& get_instance();
            $CI->{$model} = new $class;
        }
    }
    
    // https://github.com/bcit-ci/CodeIgniter/blob/b862664f2ce2d20382b9af5bfa4dd036f5755409/system/core/Controller.php#L52
    class CI_Controller {
        private static $instance;
    
        public function __construct()
        {
            self::$instance =& $this;
            $this->load =& load_class('CI_Loader');
        }
    
        /**
         * Get the CI singleton
         *
         * @static
         * @return  object
         */
        public static function &get_instance()
        {
            return self::$instance;
        }
    }
    
    // your models
    class fooModel {
        public $foo;
    }
    
    class barModel {
        public $bar;
    }
    
    // your controller
    class Controller extends CI_Controller {
    
        public function indexAction()
        {
            $this->load->model('foo');
            $this->load->model('bar');
        }
    }
    
    // instance container
    $app = (new class{});
    $app->controller = new Controller();
    
    // loaded by router
    $app->controller->indexAction();
    
    print_r($app);
    

    Result (ignoring error):

    class@anonymous Object
    (
        [controller] => Controller Object
            (
                [load] => Loader Object
                    (
                    )
    
                [foo] => fooModel Object
                    (
                        [foo] => 
                    )
    
                [bar] => barModel Object
                    (
                        [bar] => 
                    )
    
            )
    
    )
    

    https://3v4l.org/fPCK3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?