PHP OOP - Static Methods
Static methods can be called directly - without creating an instance of the class first.
Static methods are declared with the static keyword:
Syntax
<?php class ClassName { public static function staticMethod() { echo "Hello CodeLines!"; } } ?>
Output
Example
<?php class greeting { public static function welcome() { echo "Hello CodeLines!"; } } // Call static method greeting::welcome(); ?>
Output
In this example:
Here, we declare a static method: welcome(). Then, we call the static method by using the class name, double colon (::), and the method name (without creating an instance of the class first).
PHP - More on Static Methods
A class can have both static and non-static methods. A static method can be accessed from a method in the same class using the self keyword and double colon (::):