PHP Basic Tutorial
MySQL Connection
PHP Advanced
In PHP, static methods belong to the class itself, not to a specific object of the class.
You can call a static method without creating an object.
Static methods are declared with the static
keyword:
class ClassName { public static function staticMethodName() { // code } }
You call the method using the class name, double colon (::), and the method name::
ClassName::staticMethodName();
Let's look at an example:
Try it yourself
public static function welcome()
→ Declares a static method.Greeting::welcome();
→ Calls the method without creating an object. In PHP, a class can contain both static and non-static methods. Static methods can be accessed from within the same class using the self::
keyword (with the scope resolution operator ::
).
Try it yourself
In PHP, public static methods can be accessed from outside their class, including from other classes.
Try it yourself
In PHP, when you're working with inheritance, you can call a static method from a parent class inside a child class using the parent::
keyword. This works for public and protected static methods.
Try it yourself