How to invoke an overridden method in php

Post Reply
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

How to invoke an overridden method in php

Post by Saman » 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:

Code: Select all

/**
* Get Name
*/

public function getName() {
	$temp = "Name: " . $this->firstName . " " . $this->lastName . "<br />";
	return $temp;
}
Function on child class:

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;
}
Post Reply

Return to “PHP & MySQL”