Welcome Guest, Not a member yet? Register   Sign In
help to understand the CI_Loader code
#1

[eluser]iansane[/eluser]
Hi,

I'm trying to learn more in depth about loading classes by looking at the loader.php in ci core.

One thing has me stumped. When I call $this->load->library() where is it the load function?

I've looked through several files in the core and can only find a 'load' function in core/config.php that is for loading config files.

I'm thinking since CI_Loader is loaded automatically by the system that's why I can use $this-> but I'm looking for a 'load' function in CI_Loader and don't see it. Is this one of those automagic things like __autoloader? or am I looking in the wrong place?

Thanks
#2

[eluser]CroNiX[/eluser]
/system/core/Loader.php
#3

[eluser]iansane[/eluser]
[quote author="CroNiX" date="1335145241"]/system/core/Loader.php[/quote]

Right, that's where I'm looking and I don't see a 'load' function.
#4

[eluser]CroNiX[/eluser]
load = loader
loader is instantiated as "load"
#5

[eluser]iansane[/eluser]
[quote author="CroNiX" date="1335145828"]load = loader
loader is instantiated as "load"
[/quote]

I'm sorry, I still don't see it. Can you tell me where load is defined as loader?

I see in Controller
Code:
public function __construct()
{
  self::$instance =& $this;
  
  // Assign all the class objects that were instantiated by the
  // bootstrap file (CodeIgniter.php) to local class variables
  // so that CI can run as one big super object.
  foreach (is_loaded() as $var => $class)
  {
   $this->$var =& load_class($class);
  }

  $this->load =& load_class('Loader', 'core');

  $this->load->initialize();
  
  log_message('debug', "Controller Class Initialized");
}

But don't see where the 'load' is comming from in $this->load.
#6

[eluser]CroNiX[/eluser]
It's instantiated in the /core/Controller.php
Code:
$this->load =& load_class('Loader', 'core');
#7

[eluser]iansane[/eluser]
[quote author="CroNiX" date="1335146376"]It's instantiated in the /core/Controller.php
Code:
$this->load =& load_class('Loader', 'core');
[/quote]

Thanks for your help.

For some reason I thought load would have to be defined as a method or defined as a var like
Code:
public load;
before you could call
Code:
$this->load

Guess I learned something new. I've been wasting time initializing variables at the top of classes or before calling them with $this
#8

[eluser]Ajaxboy[/eluser]
you were not wasting time, the right way to do it is by declaring them on the top of the class, otherwise it creates confusion such this very confusion you had.
#9

[eluser]iansane[/eluser]
[quote author="Ajaxboy" date="1335147906"]you were not wasting time, the right way to do it is by declaring them on the top of the class, otherwise it creates confusion such this very confusion you had. [/quote]

True, 'waste' is a bad word for me to use. I do like to be precise and follow best practices for the sake of consistency and readability, but it is nice to know I can do it this way if it's appropriate in a situation.
#10

[eluser]Ajaxboy[/eluser]
Yeah it is good habit to declare them, I would only not do it if the variable was never used anywhere else (and even then I probably would just make it private or restricted). A broad variable such as $load would definitely have to be on top of the class!.




Theme © iAndrew 2016 - Forum software by © MyBB