在CodeIgniter中重新加载需要自定义配置

时间:2016-11-29 17:50:49

标签: php codeigniter

我在CI中有一个库,需要动态配置对象来设置它。

可以访问配置对象的另一个库需要进一步使用相同的库。

目前,这样可以正常工作,因为第二次加载库会返回最初创建的单例。但是,这并不是很好,因为它只能用于以前使用config对象调用库。它不可移植,好像最初没有调用库那么它会出错。

这是设置....

controller_a

$this->load->library('lib_a');
$this->lib_a->do_something();

然后在lib_a中我们动态地创建一个配置对象..

// loads lib_b
$this->load->library('lib_b');

// and loads `shared_lib`
$my_config = $this->build_config();
$this->load->library('shared_lib', $my_config);


// call something in shared_lib
$this->shared_lib->do_something();


// call something in lib_b
$this->lib_b->do_something();

然后在lib_b我们也会调用shared_lib

// Load the shared lib, but we don't have access to the config.
// But this is fine as it returns the existing singleton instance
$this->load->library('shared_lib');
$this->shared_lib->do_something_else();

有哪些替代方案/更好的方法可以做到这一点?

  • 以某种方式添加一些验证/错误处理,这样如果加载了库并且已经配置了HASNT,我可以抛出Exception吗?

  • 在所有函数调用周围传递配置?

  • 创建一个静态类来创建和检索单例?
    Shared_lib::create( $config );开始时创建 然后使用Shared_lib::get_instance();

    进行检索

    但这几乎有同样的问题......

  • 创建lib并传递它而不是?

必须有一种更清洁的方法来解决这个问题......

0 个答案:

没有答案
相关问题