How to invoke an overridden method in php
Posted: Sun Jul 17, 2011 9:52 am
The parent keyword can be used with any method that overrides its counterpart in a parent class. When you override a method, you may not wish to obliterate the functionality of the parent but rather extend it. You can achieve this by calling the parent class’s method in the current object’s context.
Function on parent class:
Function on child class:
Function on parent class:
Code: Select all
/**
* Get Name
*/
public function getName() {
$temp = "Name: " . $this->firstName . " " . $this->lastName . "<br />";
return $temp;
}
Code: Select all
/**
* Get Name
* Invoking an Overridden Method
*/
public function getName() {
$temp = parent::getName();
$temp .= " An overridden example with parent and child data. ";
return $temp;
}