• 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class instantiation in codeigniter

How inbuilt classes like libraries,views are instantiated in codeigniter ? 

eg:using new operator

With $this->load->library('lib_name');

... and then, once you have loaded the class, you can

$x = new Lib_name();

Works for models too, but not for views, since they are parsed by the Output or Parser classes.

And also understand that a call to
PHP Code:
$this->load->library('lib_name'); // or load->model('lib_name'); 
instantiates the class using new.

The instance created is added to the controller and is accessed using
PHP Code:
anywhere the controller is in scope.

In a case where the controller is not in scope a reference to can be obtained using get_instance() eg.
PHP Code:
$CI =& get_instance(); 

The Utilizing CodeIgniter Resources within Your Library section of the documentation explains this very well.

It may be useful to know that classes instantiated using the loader ($this->load->...) are singletons.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

Users browsing this thread:
2 Guest(s)

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.