Search code examples
phpclassooppolymorphismprivate

Private and protected works on child class. and what is polymophism


<?php
class main{
  public $bob="my name bob";
  private $lee="my surname lee";
  protected $david="my caste";
       function output(){
  $output=$this->lee;
   echo $output;
}
} class second extends main{ } $obj= new second; $obj->output();

?>

the output is 'my surname lee'. how ? i am confused with private and protected.please let me understand the working of it.and what is polymorphism in php any tutorial link or example.


Solution

  • polymorphism in php is well explained here.

    In one word: Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface.

    For your question: However the variable

    $lee
    

    is private, the function

    function output()
    

    is public and can be accessed outside.

    And this page have more specific sinariao in the php programming language.