PHP Class Inheritance
In this chapter you will learn:
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:
- What is Overriding Methods
- How to override a method
- Example - subclasses
- Preserving the Functionality of the Parent Class
Home » PHP Tutorial » PHP Class Definition