Arumand
Arumand

Reputation: 1894

Accessing CI features on non-CI class

So for example, i want to access config on CI from my class library.

Class A {
  funcion x() {
   $this->config->load('my_config'); // accessing my_config
  }
}

that obviously won't work unless you extends and then call parent::__construct().

As far as i know, it can only be done from classes that extend CI_Controller or CI_Model. How to access CI stuff (config, helper, model, etc) on non-CI class ?

Thanks.

Upvotes: 1

Views: 45

Answers (2)

Ohgodwhy
Ohgodwhy

Reputation: 50777

You probably want to access the instance of CI as if it were the super variable, $this. In reality what you need is the ability to access the same functionality as $this and in order to do this, you'll need to use $CI =& get_instance();

You can find it directly in the documentation for Creating Libraries

Upvotes: 1

Daryl Gill
Daryl Gill

Reputation: 5524

How about initiating the class from the construct?

  include ("CI_Page.php");
class A {
     protected $_CIInstance1;
     protected $_CIInstance2;
    public function __construct(){ 

     $this->_CIInstance1 = new xxxx();
     $this->_CIInstance2 = new yyyy();
    }

    public function x(){
      $this->_CIInstance1->load('my_config');
    }
}

Upvotes: 1

Related Questions