Static Keyword in PHP
In the last article I have explained about one of the OOPs concept is Type hinting and today i am going to explain one more OOPs concept and its static keywords in newer PHP that is PHP5.
PHP5 offers a static methods and static member variables.A special kind of method that can be called without instantiating a class.Static methods don’t depend on the properties of a particular instance of a class but belong to the class itself.
Static methods are called using the syntax shown Below:
|
1 |
ClassName::method() // :: known as scope resolution operator |
Static methods are executed only when they are called so there is no need of object creation so we can save some memory which object can occupy.
NOTE: Inside static methods, $this is not available so If you need to access a static property inside a class, you can use keyword self or parent.
Self keywords refer to the current class and and parent keyword refer the parent of the current class.Self and Parent allows you to avoid having to explicitly reference the class by name.
A static property is a class variable that is related to the class, rather than with an object of the class. This means that when it is changed, its changes are reflected in all instances of the class. Static properties are declared with the static keyword and are accessed via the syntax.
|
1 |
ClassName::$property |
To assign values to static variables, you need to use scope resolution operator(::) along with the class name. Important thing with static variable is, it has one copy of variable at class level so it has 1 unit memory location in memory of object.variable is dynamic for class,not for object.
Remember:
- We can’t access Static properties through the object using the arrow operator ->.
- Calling non-static methods statically generates an E_STRICT level warning.
Basic Example
Lets take quick example of using static
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Cl_static {
static public $var = 'Bhumi'; // declare static properties
static function md_static($var){
echo self::$var."<br/>" ;
self::$var = $var; //To change the value of the $var static property
echo self::$var."<br/>" ;
}
}
$obj = new Cl_static();
$obj->md_static("CreativeDev");
//$obj->var; //Strict Standards: Accessing static property Cl_static::$var as non static - NOT ALLOWED
Cl_static::md_static("CreativeDev"); |
Output:
|
1 2 3 4 |
Bhumi
CreativeDev
CreativeDev
CreativeDev |
In Above example,I have declared variable and method both as static so we can access both the method and the variable by using the code, Cl_static::var and Cl_static::md_static(). Because both methods and variable are declared static so you cannot access them using a instantiated object(using the arrow operator) as per PHP Manual.but above class Cl_static will throw an error for variable like
|
1 2 |
$obj = new Cl_static();
$obj->var; |
but method calling like
|
1 |
$obj->md_static("CreativeDev"); |
is allowed and as per php manual, static methods are act at the class level whereas non static methods act at the object level but if you go through above code ,you can get that static methods are working with object also.
NOTE: Static methods are 4 times faster than normal methods but its not 100% static like in java








Why do not you talk about Last Static Binding (http://php.net/manual/en/language.oop5.late-static-bindings.php) ?
It was a big conceptual error made with the OOP mode of PHP 5 that has been corrected with PHP 5.3