Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to inherit the properties and methods of base class to the child class in php

    • 0
    • 1
    • 1
    • 2
    • 0
    • 0
    • 0
    • 0
    • 209
    Comment on it
    <?php
    class data<br>
    {<br>
            public $firstno;<br>
            public $secondno;<br>
            public function getData($fno,$sno)<br>
            {<br>
            $this->firstno=$fno;<br>
            $this->secondno=$sno;<br>
            }<br>
    }<br>
    class Add extends data<br>
    {<br>
            public $sum;<br>
            public function getAdd()<br>
            {
              $this->sum=$this->firstno + $this->secondno;<br>
            }<br>
            public function show()<br>
            {<br>
              echo "The sum of $this->firstno and $this->secondno is $this->sum";<br>
            }<br>
    }<br>
    $obj=new Add();<br>
    $obj->getData(12,45);<br>
    $obj->getAdd();<br>
    $obj->show();<br>
    ?><br><br>
    

    Extends:-
    Sometimes we need some class with similar variables and functions to another existing class. The extended or derived class has all variables and functions of the base class that is called 'inheritance' and what you add in the extended definition. It is not possible to subtract from a class, that is, to undefine any existing functions or variables. An extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Classes are extended using the keyword 'extends'.

    Creating a child class:-
    To create a child class, you must use the extends keyword in the subclass declaration.
    Syntax:

    class ChildClass extends ParentClass {
            // subclass body
    }


    In above example:-

    class data{} //Creates Base or Parent class which is used by child class.

    $firstno; $secondno; // Base or Parent class properties.

    getData($fno,$sno) //parent class method with arguements $fno and $sno.

    class Add extends data //create child or derived Add .This is also called a "parent-child" relationship.

    You create a class, parent, and use extends to create a new class based on the parent class: the child class. You can even use this new child class and create another class based on this child class.

    getAdd();show() //These are methods of child or drived class.

    $obj = new Add(); // Create a Add (instance of class).

    $obj->getData(12,45); // inherited functionality from base.

    $obj->getAdd(); // Adding of two numbers in method getAdd();

    $obj->show(); // Displaying the output of adding of two numbers.

 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: