Maneesh M S
Maneesh M S

Reputation: 367

Codeigniter Custom Library loading error

My custom Library not loading through auto loading or through manual code. I checked this one and Documentation.

library/Faq.php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Faq {
    public function __construct(){
        $this->load->model('Faq_model');
    }
    public function recentFaqs(){
        return $this->Faq_model->getFaqs();
    }
}

Loaded the library $this->load->library('faq');

Called its function $this->faq->recentFaqs();

I got the following error A PHP Error was encountered

Severity: Notice

Message: Undefined property: Faq::$faq

Filename: controllers/Faq.php

Line Number: 17

Upvotes: 0

Views: 282

Answers (1)

DFriend
DFriend

Reputation: 8964

The problem is probably because your constructor is trying to load a model using $this. But $this needs to be a reference to CodeIgniter. So, you need to obtain a CodeIgniter reference before loading the model.

class Faq {
  public $CI;

  public function __construct(){
   $this->CI = & get_instance();
   $this->CI->load->model('Faq_model');
  }

  public function recentFaqs(){
    return $this->Faq_model->getFaqs();
  }
}

The reference to CI $this->CI should be used in any function of this class that will call codeigniter classes. By saving it as a property of your class it becomes easy to reuse.

Upvotes: 2

Related Questions