Take the 2-minute tour ×

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->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?

share|improve this question
add comment

1 Answer

up vote 10 down vote accepted

It seems like you missed an underscore for your constructor:

instead of

public function _construct {

you should use

public function __construct {
share|improve this answer
Good catch. It's only returning "Key=" because the _construct function never gets called. Therefore $this->key is still null. –  treeface Oct 6 '10 at 16:37
Thank you!! I thought I was going insane. Also, I had a typo in the code... it should be $this->ci->load->config('mylib'); –  TerryMatula Oct 6 '10 at 16:52
add comment

Your Answer


