0

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

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.