Static method in PHP is very useful features. Static function and variable breaks a lot of power available to 0bject oriented programming. These methods can be directly accessible without creating any object of class. To declare a static method or static variable use static keyword. Your class will be static only if all the methods and properties are static. Static method and properties are treated as public by default (if there is no visibility defined).
Static functions may be stateless. One of the reason using static methods is that if they don’t use state, they will work local variables supplied as parameter then should be static.
Note: When properties and methods are accessed from outside the class scope then use the class name followed by two colon (::) and then the name of the property.
Calling a Dynamic method statically
Calling a dynamic method statically will generates a E_STRICT error, as dynamic method assume to be in object and with static it cannot be possible as in static class object cannot made. Methods to be called statistically don’t have access on $this and using it will generate an error to warn us that something is incorrect.
Static methods of a class is directly accessible from the class using two colon (::) which is known as scope resolution operator. You can declare static function or method by using static keywords. In other words, you can make any function/ methods by using static keywords. If you are not specifying any visibility then by default it is considered as public. Following is an example to show how static method works:
static function xyz($par1 , $par2) // two parameter are passed
echo "$par1 , $par2"; // display the value of the parameter
text::xyz("Neha" , "Mehta"); //:: colon is used to access the class directly
As you can see in above example, after defining static method you don’t need to make class object. You can access that property (xyz) from class with the help of two colon:: (scope resolution operator).
Static Properties/ variable
Static property/ variable of a class is directly accessible from the class using two colon (::) which is known as scope resolution operator. You can declare static property/ variable by using static keywords. In other words, you can make any property/ variable by using static keywords. If you are not specifying any visibility then by default it is considered as public. Following is an example to show how static variable works:
private static $var1 = 0;
public function __construct()
self::$var1 = self::$var1 + 1;
echo "Hello World: ". self::$var1."</br>"; }
$ob1 = new test();
$ob2 = new test();
$ob3= new test();
Hello World: 1
Hello World: 2
Hello World: 3