Static variables and methods is very useful feature in Php oops. We can access static method and variable outside and within class, without creating object of these class. Often, we hear about static class, but there are no class is static, actually when all methods and properties is static within a class then it will be called as a static but here are not any static keyword for class. Static variable and method in Php oops will be treated as public if no-access modifiers is defined.
We use Scope resolution operator (::) to access the property of static variable or method. Static properties of any class is a property that is directly accessible from class. We can declare static property using static keyword.
Here is the basic example of static variable and static method.
<?php
class Hello
{
public static $first; //Static variable
public static function helloWorld()
{
echo "This is the static funvtion of Hello Class";
}
}
Hello::$first = 5;
echo Hello::$first;
echo "<br/>";
Hello::helloWorld();
?>
Above simple example states declaration of the static keyword.
0 Comment(s)