This article is different because it deals with only the creation of a static method. Before we continue, we will insure we know the difference between a non-static class method and a static method.
Non-Static Method:
Static Method:
The real world example of the differences between a non static method and static method can be an old full sized computer tower. In the static method point of view, assume you only want to use the computer tower as a foot rest when you sit at your desk. For this, you do not need to turn on the computer (create a class object). This does not require you to load you operating system (no instantiating of class variables required). But, you can still kick up your feet. However, because you did not turn on your computer, you will be unable to access non-static methods (can only call on static method methods).
In the non-static method point of view, if you want the operation system, you would need to turn on the computer (create class object). What this does is load your operating system (instantiate class variables) so you can finally login to your computer (method of the class). Additionally, with the computer on, we can still kick our feet on the tower (can call both static and non static function).
Now how do we create a function with static methods in PHP? When we write the static method, we must use the keyword static. This method we create will always return true and is created in the common class.
class common {
public static function myStaticFunction()
{
return true;
}
}
Now that we have created the static function, we can call on the static method with the following snippet of code. The first term “common” is the name of the class. This is followed by two colons (::) followed by the method name.
$value = common::myStaticFunction();
And there we have it! We have successfully created and used our static method. To see the official PHP article on static, go to http://www.php.net/manual/en/language.oop5.static.php or http://www.victorchen.info/how-to-create-php-static-method/
Facts
Declaring class members or methods as static makes them accessible without needing an instantiation of the class
Static properties cannot be accessed through the object using the arrow operator ->.
the pseudo variable $this is not available inside the method declared as static.
Calling non-static methods the way you call static methods generates Warnings
Unlike regularmethods and properties, their static counterparts exist and are accessible as part of a class itself, as opposed to existing only within the scope of one of its instances. This allows you to treat classes as true containers of interrelated functions and data elements—which, in turn, is a very handy expedient to avoid naming conflicts.