PHP INHERITANCE

Sugar babies

 PHP INHERITANCE

                 •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.
                    •In addition, it can have its own properties and methods. 
                    •An inherited class is defined by using the extends keyword.

TYPES IN INHERITANCE :

                • Single Inheritance.
                •Multiple Inheritance.
                •Multilevel Inheritance.
                •Hierarchical Inheritance.
                •Hybrid Inheritance.

SYNTAX FOR INHERITANCE IN PHP :

class derived_classname : access base_classname{ /*define class body*/ };

 EXAMPLE FOR INHERITANCE IN PHP :

<?php
/* Parent Class (also called superclass and base class)*/
class ParentClass
{
  function a()
  {
    echo "A member function of the base class.";
  }
}
/* Child class (also called Derived class, extended class and subclass)*/
class ChildClass extends ParentClass
{
  function b()
  {
    echo "A member function of the child class.";
  }
}
$Obj = new ChildClass(); /* Create object (child class instance) */
$Obj->a(); /*call member function of ParentClass*/
$Obj->b(); /*call member function of ChildClass*/
?>

OUTPUT :


A member function of the base class.
A member function of the child class.
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send