CodeIgniter Forums
Class instantiation in codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Class instantiation in codeigniter (/showthread.php?tid=70244)



Class instantiation in codeigniter - Santi - 03-13-2018

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

eg:using new operator


RE: Class instantiation in codeigniter - dave friend - 03-13-2018

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


RE: Class instantiation in codeigniter - ciadmin - 03-13-2018

... 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.


RE: Class instantiation in codeigniter - dave friend - 03-16-2018

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.