Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • INHERITANCE IN PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 172
    Comment on it

    Inheritance is one of the most important concept of OOP. In inheritance one class works as a base class for one more than one class. In this derived class inherits all the properties and methods of base class.

    Inheritance makes our work easier, by writing the code once and using it in another class with the help of inheritance.

    In PHP code is inherited by using the keyword extends.

    Synatx of inheritance:-

    class baseClass {
      // properties and methods
    }
    
    class derivedClass extends baseClass {
      // additional properties and methods
    }

    E.g of inheritance:-

    class HTML
    {
    protected $name;
    public $id;
    private $with;
    protected function basicAttribute
    {
    return "name='$this->name' id='$this->id'";
    }
    }
    Class HTML_div extends HTML
    {
    public function __construct($id , $name)
    {
    $this->id = $id;
    $this->name = $name;
    }
    public function getDiv($content)
    {
    $basicAttribute = $this->basicAttribute();
    return "<div $basicAttribute >$content</div>"
    }
    }'
    $objDiv = new HTML_div("bloc_main" , 'avc');
    $objDiv->getDiv('this is and example of inheritance in php');

     

    PHP supports multilevel inheritance but not multiple inheritance. In multilevel inheritance derived class can inherit some properties of grandparent class also.

    Syntax of multilevel inheritance:-

    class grandParent
    {
    //Body of your class
    }
    class parent extends grandParent
    {
    //Body Of your class
    }
    class child extends parent
    {
    //Body of your class
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: