inheritance example : Inheritance « Class « PHP






inheritance example

 
<?php
class Bird {
  private $name;
  private $breed;
  private $price;
  
  public function __construct($name, $breed, $price = 15) {
    $this->setName ( $name );
    $this->setBreed ( $breed );
    $this->setPrice ( $price );
  }

  public function birdCall() {
    printf ( "<p>%s says: *chirp*</p>\n", $this->getName () );
  }
  public function display() {
    printf ( "<p>%s is a %s and costs \$%.2f.</p>", $this->getName (), $this->getBreed (), $this->getPrice () );
  
  }
}

class Parrot extends Bird {
  public function birdCall() {
    printf ( "<p>%s says: *squawk*</p>\n", $this->getName () );
  }
  public function __construct($name) {
    parent::__construct ( $name, 'parrot', 25 );
  }
  public function curse() {
    printf ( "<p>%s</p>\n", $this->getName () );
  }
}
?>
  
  








Related examples in the same category

1.subclass and parent class
2.Three levels of inheritance
3.extends and implement
4.Basic Inheritance
5.Class Inheritance
6.Class Member Binding in PHP
7.Creating a Class That Inherits from Another
8.Define an Executive class that inherits Employee
9.Using the extends keyword to define a subclass
10.Example of Inheritance
11.Overriding parent methods
12.Using inheritance to efficiently represent various vehicle types
13.Using the parent construct