You are on page 1of 3

PHP INHERITANCE

Continue..
⚫ <html>
⚫ <body><?php class Student {
⚫ public $name;
⚫ public $gender;
⚫ public function __construct($name, $gender) {
⚫ $this->name = $name;
⚫ $this->gender = $gender; }
⚫ protected function intro() {
⚫ echo "The Student is {$this->name} and the gender is {$this->gender}.";
⚫ }
⚫ }
class Info extends Student {
⚫ public function message() {
⚫ echo "I'm a Student ";
⚫ $this->intro();
⚫ }
⚫ }
⚫ $info = new Info("Mohit", "Male"); // OK. __construct() is public
⚫ $info->message(); // OK. message() is public
⚫ //$info->intro();
⚫ ?>
⚫ </body></html>
Inheritance
⚫ Inheritance is most poppular OOP Concept.
⚫ Inheritance in OOP = When a class derives from another class.
⚫ The child class will inherit all the public and protected properties and
methods from the parent class.
⚫ An inherited class is defined by using the extends keyword.

Syntax
class B extends A
where A is the base class (also called parent called) and B is called a
subclass or child class.
Child class inherits public and protected methods of parent class.

You might also like