Posted by: phillipnb | August 15, 2011

Use of static keyword in PHP


Suppose we have a class like this:

class A {
    public static $var1 = 'a';
    public static function sayHello() {
        echo "hello";
    }
}

According to PHP manual “Declaring class properties or methods as static makes them accessible without needing an instantiation of the class”. That means for the above example, we can access both the method and the variable by using the code, A::var1 and A::sayHello(). Both the variable as well as the method is declared as static and hence you cannot access them using a instantiated object(using the arrow operator). for e.g. for the above class A, the following code will throw an error.

$objA = new A;
$objA->var1; 

but $objA->sayHello() is allowed.

The static method ‘sayHello’ can be called using the code A::sayHello() and hence the pseudo variable $this is not available inside the method ‘sayHello’. The manual also says that “Like any other static variables, static properties may only be initialized using a literal constant, expressions are not allowed. You can initialize static property to an integer or array, but you may not initialize it to another variable, to a function return value or an object”.

So, what did we learn about static properties and static methods? The php manual does answer this question. According to the manual, static methods and static properties act at the class level whereas non static methods act at the instance level. Also, if we need to use a container for methods that deal with external data then we might want to think of using static methods.

Using variable variables to access static properties

Consider the above class ‘A’. To access the property ‘var1’, we used A::$var1. We can implement the same thing using variable variables. Both the class name as well as the property name can be stored in variables. For example, Take a look at this piece of code:

class A {
    public static $var1 = 'a';
    public static function sayHello() {
        echo "hello";
    }
}
$classname = 'A';
$temp1     = 'var1';

echo $classname::$var1;     // valid
echo "\n";
echo $classname::${$temp1}; // valid

We can also access class methods using variables. for example:

$temp2 = 'sayHello';
$temp3 = 'sayHello()';
$classname = 'A';

echo $classname::$temp2(); // valid
echo "\n";
echo $classname::sayHello(); // valid
echo "\n";
But echo $classname::$temp3 is invalid

Which of these is faster – classname , static or self?

Based on the above example, if we are accessing a class static property from outside the class, then the usage $classname::$var1 is faster than A::$var1 whereas in case of accessing methods the usage A::sayHello() is slightly faster than $classname::sayHello(). But if you are accessing a static variable from within the class A::$var1 is faster than self::$var1,static::$var1 and $classname::$var1. Also, from the experiments conducted to understand the speed of each type of usage it was found that A::sayHello() is faster than self:sayHello(),static::sayHello() and $classname::sayHello().

Alright – so far I hope i have given you a brief tour about the usage of the word ‘static’ in php. As usual, please post your valuable comments and suggestions.

Till next time, it is happy PHPing.

Advertisements

Responses

  1. The $classname::${$temp1}; trick was just what I was looking for — thanks for posting!

    • Happy to know that the write-up was useful to you – Phillip.

  2. The way example and the explanation should be useful for me. beacase I am searching for this answer two days before saw this web. Thanks buddy.

  3. Good Explanation!!!!!

  4. Perfect answer what is was looking for..
    thanks

  5. helpful one


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: