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