Home > Article > Backend Development > How to implement an object calling the method of its parent class in PHP
In PHP, an object can call its own methods and methods inherited from parent classes. In some cases, we want to call the method of its parent class in a method of a subclass. In this case, we need to use the method of the PHP object to call the parent class method.
Let’s discuss how to implement the method of an object calling its parent class in PHP.
1. Call parent class methods through the parent keyword
In PHP, there is a keyword parent
that can be used to call methods in the parent class. By using the form parent::method()
in the subclass method, you can call the method of the same name in the parent class.
For example, we have the following parent and child classes:
class Animal { public function eat() { echo 'Animal is eating...'; } } class Cat extends Animal { public function eat() { parent::eat(); echo 'Cat is eating...'; } }
In the above code, the Animal
class has an eat()
method, while the Cat
class inherits the Animal
class and overrides the eat()
method. In the eat()
method of the Cat
class, the eat()
method of the parent class is called through parent::eat()
.
Calling the eat()
method of the Cat
class will output Animal is eating...Cat is eating...
.
2. Use $this->parent
to call the parent class method
In addition to using the parent
keyword, in PHP 5.3 or above, You can also call parent class methods in the form $this->parent
.
For example, we can change the above Cat
class to the following form:
class Cat extends Animal { public function eat() { $this->parent::eat(); echo 'Cat is eating...'; } }
Here we use the $this->parent
call The eat()
method of the parent class is added, and Cat is eating...
is output.
3. Summary
By using the parent
keyword or $this->parent
, we can easily implement the object in PHP to call its Methods of the parent class.
It should be noted that when there are variable parameters in the parent class method, for example:
class Animal { public function eat($food1, $food2) { echo "Animal is eating $food1 and $food2"; } } class Cat extends Animal { public function eat($food1, $food2) { //error, 不可使用parent::eat($food1, $food2) } }
In the subclass, it is not allowed to use parent::eat($food1 , $food2)
to call the method of the parent class. Because variable parameters in the parent class may affect the behavior in the subclass, PHP does not allow this method. The parameters must be redefined in the subclass method, for example:
class Cat extends Animal { public function eat($food1, $food2) { parent::eat($food1, $food2); echo "Cat is eating $food1 and $food2"; } }
The above is the object call in PHP A brief introduction to the methods of the parent class, I hope it will be helpful for readers to understand how PHP objects call parent class methods.
The above is the detailed content of How to implement an object calling the method of its parent class in PHP. For more information, please follow other related articles on the PHP Chinese website!