14

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 Answer 1

17

It seems like you missed an underscore for your constructor:

instead of

public function _construct () {

you should use

public function __construct () {
Sign up to request clarification or add additional context in comments.

2 Comments

Good catch. It's only returning "Key=" because the _construct function never gets called. Therefore $this->key is still null.
Thank you!! I thought I was going insane. Also, I had a typo in the code... it should be $this->ci->load->config('mylib');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.