Override private member in PHP

Description

The following code shows how to override private member.

Example


// w  w  w  .  ja  va  2s  .c o  m
<?php
  class Father
  {
    private $salutation = "Hello there!";

    public function getSalutation()
    {
      print("$this->salutation\n");
      $this->identify();
    }

    private function identify()
    {
      print("I am Father.<br>\n");
    }
  }

  class Son extends Father
  {
    private $salutation = "Hey!";

    private function identify()
    {
      print("I am Son.<br>\n");
    }
  }

  $obj = new Son();
  $obj->getSalutation();
?>

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