PHP Class Inheritance

In this chapter you will learn:

  1. What is Class Inheritance
  2. Syntax to create Inheritance
  3. Example - create Inheritance

What

With inheritance we can create child classes that are based on the parent class.

A child class inherits all the properties and methods of its parent, and it can also add additional properties and methods.

Syntax

To create a child class that ' s based on a parent class, you use the extends keyword, as follows:


<?PHP/*j a  va  2s. c o  m*/
class Shape { 
   // (General Shape properties and methods here) 
} 
              
class Circle extends Shape { 
   // (Circle-specific properties and methods here) 
}     
?>

Example

To extend the Dog class we can use extends keyword.


<?PHP//from  ja v  a2  s  . c om
class Dog {
    public function bark() {
       print "Woof!\n";
    }
}

class Puppy extends Dog {
   
}
?>

Next chapter...

What you will learn in the next chapter:

  1. What is Overriding Methods
  2. How to override a method
  3. Example - subclasses
  4. Preserving the Functionality of the Parent Class
Home » PHP Tutorial » PHP Class Definition
Concept for Object Oriented Design
PHP Class Definition
PHP Create Object from Class
PHP Class Properties
PHP Iterating Object Properties
PHP Class Inheritance
PHP Overriding Methods
PHP final Classes and Methods
PHP Abstract Class
PHP Class Access Control Modifiers
PHP Class Constructor
PHP Class Destructors
PHP Class Magic Methods