Class Inheritance with PHP | WebReference

Class Inheritance with PHP

Class Inheritance with PHP

By Kris Hadlock

Digg This Add to del.icio.us

There are many benefits of inheritance with PHP, the most common is simplifying and reducing instances of redundant code. Class inheritance may sound complicated, but think of it this way. Consider a tree. A tree is made up of many parts, such as the roots that reside in the ground, the trunk, bark, branches, leaves, etc. Essentially inheritance is a connection between a child and its parent.

Note: This article assumes a basic understanding of OOP with PHP. You'll also need a server with PHP 5 in order to run this demo, which you can download here.

Creating a parent object

Let's say that our intention is to create a storefront where we'll be selling cars. A simple shopping cart allows us to sell the cars and allows consumers to browse the products, and access specific information such as pricing and a description. With this in mind we know that regardless of what we're selling, all products have certain things in common, such as a name, description, price and photo. By inheriting the Product object we can share these common properties across unique child objects.

The Product object is quite simple, it allows us to define specific information such as a name, price, photo and description. The objects setters allow us to set these specific product properties, and by using the protected keyword we only allow these properties to be set by extending child objects. Also, the Products getters are public, which allows us to obtain specific data from an object with any PHP page that includes and instantiates it. The last thing to note about this object are the properties defined at the beginning of the class. We define these as private properties so they can't be changed directly unless using the setters provided by the Product object, this provides more security and reliability. Let's look at how to inherit the Product object.

Inheriting the parent object

Inheriting a parent object is probably one of the easiest concepts in object-oriented programming. All we need to do to inherit the Product object is add the extends keyword after the class name, followed by the object that we want to inherit, in this case the Product object.

By taking a look at the car object we see how simple it's become by creating the parent to handle all of the product details. Now we can use this object for custom properties that a car includes, which are not common to all products. As an example, we add a model property to specify the model of the car. Another benefit is being able to scale this application by adding new products that inherit the Product object or we could add more common properties to the Product object. The addition of properties will enhance all child objects without interfering with their existing ties.

Created: August 20, 2007
Revised: September 12, 2007

URL: https://webreference.com/programming/php/class_inherit