Example of Inheritance : Inheritance « Class « PHP






Example of Inheritance

 
<?php
class cd {
    public $artist;
    public $title;
    protected $tracks;
    private $disk_id;

    public function __construct() {
        $this->disk_id = sha1('cd' . time() . rand());
    }
    public function get_disk_id() {
        return $this->disk_id;
    }
}

class cd_album extends cd {
    protected $num_disks;

    public function __construct($disks = 1) {
        $this->num_disks = $disks;
        parent::__construct();
    }
    public function is_multi_cd() {
        return ($this->num_disks > 1) ? true : false;
    }
}
$mydisk = new cd_album(3);
echo '<p>The compact disk ID is: ', $mydisk->get_disk_id(), '</p>';
echo '<p>Is this a multi cd? ',
    ($mydisk->is_multi_cd()) ? 'Yes' : 'No',
    '</p>';
?>
  
  








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.inheritance example
10.Using the extends keyword to define a subclass
11.Overriding parent methods
12.Using inheritance to efficiently represent various vehicle types
13.Using the parent construct