Welcome Guest, Not a member yet? Register   Sign In
Question about class loading
#1

[eluser]hykoh[/eluser]
Hello,

i think it's a stupid question, but:

if i wrote $this->load->library('xxx'); in the __construct() { } of my controller, it will be always included right ?

What happens if I write the following code:

Code:
class Example extends Controller
{
function __construct() {
parent::__construct();
}

function index() {
$this->load->view('xxx');
}

function show() {
$this->load->library('xxx');
}

function edit() {
$this->load->view('xxx');
}
}

and i only call www.example.com/example/edit, get the library included then ? I think the whole file will parsed everytime mh ? so I can take my load for libraries if needed directly into the construct ? Or is it just loaded if i call www.example.com/example/show ???

Thanks for help!
#2

[eluser]OES[/eluser]
If you include it in your construct then it will be available to all methods (functions).

But if you only load a library in a method it will only be available to that one.

Hope that helps.
#3

[eluser]hykoh[/eluser]
i know that the library is only available in the method i load it in, but my question is if it will be included everytime ? I thought $this->load->xxx('yyy'); is nearly similar with include('xxx/yyy.php') and inititing yyy class, isnt it ?

I just want to know if $this->load->library('xxx') is included on every load ?

For the above example; if i wrote $this->load->library('xxx'); in index() and edit() method too, is it included 3 times then ?
#4

[eluser]xwero[/eluser]
You are right stating load is similar to include that is the base of most load methods. The load methods prevent files from being included more than once.

The only difference between loading files in the construct method or in the other methods is the scope. In the construct method you globally, global here is the controller, load the file while in other methods you load it locally.
#5

[eluser]hykoh[/eluser]
okay, so is it better to load it in construct or locally in the method ?
- if i put it in the construct(), it will be loaded and automatically initiated at every reload at this controller

- if i put it in the methods needed, it will be loaded X times (but dont get initiated - only if the called method load it locally, then 1 times)

So what is the best dealing with ?
#6

[eluser]xwero[/eluser]
It doesn't matter, from a loading point of view, if you put it in the construct method or in an other method because the library gets loaded only once. Even if you write something like
Code:
function __construct() {
parent::__construct();
$this->load->library('xxx');
}

function show() {
$this->load->library('xxx'); // this will be disregarded
}

From the memory point of view you better only load the library when it's needed. That means load it in the methods that need it and only load it in the construct method if (nearly) all other methods of the controller need it.




Theme © iAndrew 2016 - Forum software by © MyBB