PHP Class 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// w  w  w. j  av a2s.co 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  w  w  w . j a v  a 2s  . c  o m
class Dog {
    public function bark() {
       print "Woof!\n";
    }
}

class Puppy extends Dog {
   
}
?>




















Home »
  PHP Tutorial »
    Language Basic »




PHP Introduction
PHP Operators
PHP Statements
Variable
PHP Function Create
Exception
PHP Class Definition