问题 将自定义配置文件加载到Codeigniter库中


我知道这可能很简单,但我没有得到。我已经创建了一个库,我想从配置文件中加载参数。所以这是我的一个例子:

// libraries/Mylib.php
class Mylib {
   var $ci;
   var $key;
   public function _construct {
     $this->ci =& get_instance();
     $this->ci->config->load('mylib');
     $this->key = $this->ci->config->item('key');
   }
   public function myKey() {
     return "Key=" . $this->key;
   }
}

// config/mylib.php
$config['key'] = 'randomcharacters';

我加载库,并尝试打印myKey函数,但它只返回“Key =”,没有实际的键。我错过了什么?


8726
2017-10-06 15:28


起源



答案:


看起来你错过了构造函数的下划线:

代替

public function _construct () {

你应该使用

public function __construct () {

14
2017-10-06 16:35



接得好。它只返回“Key =”因为 _construct 函数永远不会被调用。因此 $this->key 仍然是空的。 - treeface
谢谢!!我以为我疯了。另外,我的代码中有一个拼写错误...它应该是$ this-> ci-> load-> config('mylib'); - TerryMatula


答案:


看起来你错过了构造函数的下划线:

代替

public function _construct () {

你应该使用

public function __construct () {

14
2017-10-06 16:35



接得好。它只返回“Key =”因为 _construct 函数永远不会被调用。因此 $this->key 仍然是空的。 - treeface
谢谢!!我以为我疯了。另外,我的代码中有一个拼写错误...它应该是$ this-> ci-> load-> config('mylib'); - TerryMatula