Welcome Guest, Not a member yet? Register   Sign In
how load_class function works?
#1

[eluser]djuric[/eluser]
ok this function is defined in Common.php and it's used throughout the system.

When defining this function, there is code like:

Code:
if ( ! function_exists('load_class'))
{
function &load;_class($class, $directory = 'libraries', $prefix = 'CI_')
{
  static $_classes = array();

  // Does the class exist?  If so, we're done...
  if (isset($_classes[$class]))
  {

There is 'passing by reference' sign here, at function definition:
Code:
function &load;_class() { // function }

And also, each time when this function is called, it's called with this same reference sign
Code:
$BM =& load_class('Benchmark', 'core');


I read somewhere about this, but I don't really understand how codeigniter benefits from this. So if can anyone tell me more about usage of this, I would greatly appreciate it Smile
#2

[eluser]djuric[/eluser]
This is probably php documentation for this http://www.php.net/manual/en/language.re...return.php

It says:

"Returning by reference is useful when you want to use a function to find to which variable a reference should be bound. Do not use return-by-reference to increase performance. The engine will automatically optimize this on its own. Only return references when you have a valid technical reason to do so"

What's the valid technical reason %-P
#3

[eluser]Aken[/eluser]
When classes are loaded, they are stored into the static $_classes array. If you use load_class() on a class that is already loaded, the function finds the already-loaded class in that array, and then passes it back by reference.

This means that any time that class is loaded to a variable, every variable will be a reference to the same class instance. Then, if changes are made to any variable, the changes will be available across the script.

http://www.php.net/manual/en/language.references.php
#4

[eluser]djuric[/eluser]
Thanks a lot Aken Smile




Theme © iAndrew 2016 - Forum software by © MyBB