Welcome Guest, Not a member yet? Register   Sign In
Class instantiation in codeigniter
#1

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

eg:using new operator
Reply
#2

(This post was last modified: 03-13-2018, 04:15 PM by dave friend.)

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

(This post was last modified: 03-13-2018, 06:04 PM by ciadmin.)

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




Theme © iAndrew 2016 - Forum software by © MyBB