Welcome Guest, Not a member yet? Register   Sign In
Static variables in codeigniter
#1

[eluser]djuric[/eluser]
I see lots of functions using static variables, I wonder is this for the sake of performance only? Eg. in Common.php, functions like:

Code:
function is_php($version = '5.0.0')
{
  static $_is_php;
  $version = (string)$version;

  if ( ! isset($_is_php[$version]))
  {
   $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
  }

  return $_is_php[$version];
}

or

Code:
function is_loaded($class = '')
{
  static $_is_loaded = array();

  if ($class != '')
  {
   $_is_loaded[strtolower($class)] = $class;
  }

  return $_is_loaded;
}

and many others.

Thanks for any useful infos, I appreciate it

#2

[eluser]PhilTem[/eluser]
In some cases it's for performance issues, in other cases it is because these functions like the is loaded don't belong to an object i.e. they are no method and cannot share an object's attribute, they need to have some variables set as static since they would be initiated again and again each time the function is called

PS:
You might want to read the PHP Guide on static variables to better understand the usage of the static keyword Wink
#3

[eluser]djuric[/eluser]
Thanks for help PhilTem

I did read everything on http://php.net/manual/en/language.oop5.static.php , and weird enough, they don't even mention here real usage of static. They are talking about access of variable, and not about how it's used in function

I had to search over the interwebs to find out how static variable really works.





Theme © iAndrew 2016 - Forum software by © MyBB