Extend class and override methods in PHP
Description
The following code shows how to extend class and override methods.
Example
<?php/*from w w w .j a va2 s . c o m*/
class Fruit {
public function peel() {
echo "<p>I'm peeling the fruit...</p>";
}
public function slice() {
echo "<p>I'm slicing the fruit...</p>";
}
public function eat() {
echo "<p>I'm eating the fruit. Yummy!</p>";
}
public function consume() {
$this->peel();
$this->slice();
$this->eat();
}
}
class Grape extends Fruit {
public function peel() {
echo "<p>No need to peel a grape!</p>";
}
public function slice() {
echo "<p>No need to slice a grape!</p>";
}
}
echo "<h2>Consuming an apple...</h2>";
$apple = new Fruit;
$apple->consume();
echo "<h2>Consuming a grape...</h2>";
$grape = new Grape;
$grape->consume();
?>
The code above generates the following result.