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

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

eg:using new operator
Reply

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

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

Code:
$x = new Lib_name();

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

#4
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:
$this->lib_name->some_method();
//or
$this->lib_name->some_property
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
2 Guest(s)


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