PHP7 OPPs Inheritance

Inheritance is the process in which one or more child class is derived from the parent class. The class inherits the methods and properties of the parent class by using the extends keyword. By using this, we can reuse the properties and methods of the base class in child classes.

Example

<?php
    class Person {
        public $days = 30;
        public function name($employee) {
            return 'Employee Name: '.$employee;
        }
    }
    class Employee extends Person {
        public function salary($basic){
            $salary = $this->days * $basic;
            return 'Current Salary: Rs.'.$salary;
        }
    }
    $obj1 = new Employee();
    echo $obj1->name('John Smith');
    echo '<br/>';
    echo $obj1->salary(600);
?>

Output

Employee Name: John Smith
Current Salary: Rs.18000

In the above example, The 'Employee' class is inherited the property and method from the 'Person' parent class.








Read more articles


General Knowledge



Learn Popular Language