Home >> PHP >> How to use static method and property in inheritance in PHP

How to use static method and property in inheritance in PHP

Child class we have explored that we can use $this->keyword to get all  property and method of parent class but if your parent or child class method is static methods or properties using self and parent keyword also this is not necessary to make methods static if you want to use self or parent keyword.
Example:-

class Father {
     public static fAge(){
            return "Age is 50";
     }
}
class Son extends Father{
     public static sAge(){
            return "Age is 20";
     }
     public function myHistory(){
            echo "My father".parent::fAge();
            echo "My ".self::fAge();
     }
}
$son=new Son();
$son->myHistory();
Output:-
My father Age is 50
My  Age is 20

Post Your Comment

Next Questions
How to use self and parent with static
How to use self and parent without static
What is visibility
What is the difference between public, private, and protected
What is final
What is final class
What is final method
What is final variable
How to print half pyramid using star
How to print half pyramid using number
How to print inverted half pyramid using star
How to print inverted half pyramid using number
How to print half pyramid using one to ten number
How to print pyramid pattern using star
How to print half pyramid pattern using star
How to print half pyramid pattern using star after 180 degree rotation
display errors
string to array
array to string
how to split string into array
How to convert array to string
Difference between exit and die
Set Cookie
Access Cookie
Remove Cookie

Copyright ©2022 coderraj.com. All Rights Reserved.