Call current class with self in PHP

Description

The following code shows how to call current class with self.

Example


<?php/*from   ww w . j a va  2 s . co 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