Reference parent class with parent in PHP

Description

The following code shows how to reference parent class with parent.

Example


<?php// w  w  w  .ja  v a 2  s  . c o  m
  class Animal
  {
    public $blood;
    public $name;

    public function __construct($blood, $name=NULL)
    {
      $this->blood = $blood;
      if($name)
      {
        $this->name = $name;
      }
    }
  }

  class Mammal extends Animal
  {
    public $furColor;
    public $legs;

    function __construct($furColor, $legs, $name=NULL)
    {
      parent::__construct("warm", $name);
      $this->furColor = $furColor;
      $this->legs = $legs;
    }
  }

  class Dog extends Mammal
  {
    function __construct($furColor, $name)
    {
      parent::__construct($furColor, 4, $name);
      self::bark();
    }

    function bark()
    {
      print("$this->name says 'woof!'");
    }
  }

  $d = new Dog("Black and Tan", "Angus");
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition