public static function welcome() → Declares a static method.
Greeting::welcome(); → Calls the method without creating an object.
PHP - More on Static Methods
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::).
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.
Example
<?php
classParentClass {
protectedstaticfunctionshowMessage() {
return"Hello from Parent!";
}
}
classChildClassextends ParentClass {
publicstaticfunctioncallParentMethod() {
return parent::showMessage();
}
}
echo ChildClass::callParentMethod(); // Output: Hello from Parent!
We use cookies and similar technologies to enhance your browsing experience, serve personalized content and advertisements, and analyze our traffic.
By clicking "Accept All", you consent to our use of cookies and the processing of your personal data for these purposes.
You can manage your preferences or learn more in our
Privacy Policy and
Cookie Policy.