Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static variables in PHP

I have found different information regarding static variables in PHP but nothing that actually explains what it is and how it really works.

I have read that when used within a class that a static property cannot be used by any object instantiated by that class and that a static method can be used by an object instantiated by the class?

However, I have been trying to research what a static variable does within a function that is not in a class. Also, does a static variable within a function work somewhat like closure in javascript or am I totally off in this assumption?

like image 989
mike Avatar asked Sep 22 '11 00:09

mike


People also ask

What is static variable with example?

The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. The static variable gets memory only once in the class area at the time of class loading.

Does PHP have static variables?

Introduction: A static class in PHP is a type of class which is instantiated only once in a program. It must contain a static member (variable) or a static member function (method) or both. The variables and methods are accessed without the creation of an object, using the scope resolution operator(::).

Which is the static variable?

In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program.

What is difference between static and global variable in PHP?

Global is used to get the global vars which may be defined in other scripts, or not in the same scope. e.g. Static is used to define an var which has whole script life, and init only once.


2 Answers

static has two uses in PHP:

First, and most commonly, it can be used to define 'class' variables/functions (as opposed to instance variables/functions), that can be accessed without instantiating a class:

class A {   public static $var = 'val'; // $var is static (in class context)   public $other_var = 'other_val'; // non-static }  echo A::$var; // val echo A::$other_var // doesn't work (fatal error, undefined static variable)  $a = new A; echo $a->var // won't work (strict standards) echo $a->other_var // other_val 

Secondly, it can be used to maintain state between function calls:

function a() {   static $i = 0;   $j = 0;   return array($i++, $j++); } print_r(a()); // array(0, 0) print_r(a()); // array(1, 0) print_r(a()); // array(2, 0) //... 

Note that declaring a variable static within a function works the same regardless of whether or not the function is defined in a class, all that matters is where the variable is declared (class member or in a function).

like image 28
connec Avatar answered Oct 10 '22 19:10

connec


I have read that when used within a class that a static property cannot be used by any object instantiated by that class

It depends on what you mean by that. eg:

class Foo {     static $my_var = 'Foo'; }  $x = new Foo();  echo $x::$my_var;  // works fine echo $x->my_var;   // doesn't work - Notice: Undefined property: Foo::$my_var 

and that a static method can be used by an object instantiated by the class???

Yes, an instantiated object belonging to the class can access a static method.

The keyword static in the context of classes behave somewhat like static class variables in other languages. A member (method or variable) declared static is associated with the class and rather than an instance of that class. Thus, you can access it without an instance of the class (eg: in the example above, I could use Foo::$my_var)


However, I have been trying to research what a static variable does within a function that is not in a class.

Also, does a static variable within a function work somewhat like closure in javascript or am I totally off in this assumption.

Outside of classes (ie: in functions), a static variable is a variable that doesn't lose its value when the function exits. So in sense, yes, they work like closures in JavaScript.

But unlike JS closures, there's only one value for the variable that's maintained across different invocations of the same function. From the PHP manual's example:

function test() {     static $a = 0;     echo $a;     $a++; }  test();  // prints 0 test();  // prints 1 test();  // prints 2 

Reference: static keyword (in classes), (in functions)

like image 87
NullUserException Avatar answered Oct 10 '22 19:10

NullUserException