Loading custom config file into a Codeigniter libr

2020-08-13 02:17发布

问题:

I know this is probably simple, but I'm not getting. I've created a library, and I want to load the parameters from a config file. So here's an example of what I have:

// 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';

I load the library, and try to print out the myKey function, but it just returns "Key=", without the actual key. What am I missing?

回答1:

It seems like you missed an underscore for your constructor:

instead of

public function _construct () {

you should use

public function __construct () {