Static Methods and Properties in PHP

Static Methods and Properties in PHP

Static methods and properties in php is very useful feature. Static methods and properties in php can directly accessible without creating object of class. Your php class will be static class if your all methods and properties of the class is static. Static Methods and Properties in PHP will be treated as public if no visibility is defined.

Static Properties/Variables in PHP

Static properties of class is a property which is directly accessible from class with the help of ::(scope resolution operator). You can declare static  property using static keyword. In other word you can make any property static by using static keyword. following is the basic example of static variable in php class:
class test
{
public static $a;//Static variable
}
test::$a = 5;
echo test::$a;

You can not access regular property by static way. It will generate fatal error. For withing the class you can access static property using self keyword. If you are accessing parent class property then you need to use parent keyword.
class testParent
{
public static $var1;
}
class testChild extends testParent
{
public static $var2;
public $abc =2;
function testFunction()
{
self::$var2 = 3;
parent::$var1 = 5;
}
}
echo testChild::$abc; //throw fatal error

Static variable or property are the best way to preserver value of the variable within the context of different instance. Please go through following example for better expatiation:

class test
{
private static $no_of_call = 0;
public function __construct()
{
self::$no_of_call = self::$no_of_call + 1;
echo "No of time object of the class created is: ". self::$no_of_call;
}
}
$objT = new test(); // Prints No of time object of the class created is 1
$objT2 = new test(); //Prints No of time object of the class created is 2

So creating static variable or property is very useful if you want to share some data between the different object of the same class. We will get better example of the static property implementation in chapter PHP Design Patterns.

Static Methods or functions

As in general class various process are same for methods and properties, same is with Static Methods and Properties in PHP. You can create your function or method static using static keyword. You can access all visible static methods for you using :: like in static variables.
class test
{
static function abc($param1 , $param2)
{
echo "$param1 , $param2";
}
}
test::abc("ankur" , "techflirt");

If you will use regular or normal method statically then you will get E_STRICT warning. In case of variable or property it was throwing fatal. Let us take above example
class test
{
function abc($param1 , $param2)
{
echo "$param1 , $param2";
}
}
test::abc("ankur" , "techflirt"); //will work fine with warning.

Since static methods is called direct $this variable will not available in the method.

Download Code for static methods in PHP.

For further details about static methods and properties in php you can go to:
http://php.net/manual/en/language.oop5.static.php
http://en.wikipedia.org/wiki/Static_method#Static_methods