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.
		<?php
		
		class Product
		{
			private $name;
			private $price;
			private $photo;
			private $description;
			
			public function Product() {}
			
			protected function setName($name) { $this->name = $name; }
			public function GetName() { return $this->name; }
			
			protected function setPrice($price = '0.00') { $this->price = $price; }
			public function GetPrice() { return $this->price; }
			
			protected function setPhoto($photo) { $this->photo = $photo; }
			public function GetPhoto() { return $this->photo; }
			
			protected function setDescription($description) { $this->description = $description; }
			public function GetDescription() { return $this->description; }
			
		}
		
		?>
	
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.
		<?php
		
		class Car extends Product
		{
			private $model;
			
			public function Car($name, $model, $price, $photo, $description)
			{
				parent::setName($name);
				$this->setModel($model);
				parent::setPrice($price);
				parent::setPhoto($photo);
				parent::setDescription($description);
			}
			
			private function setModel($model) { $this->model = $model; }
			public function GetModel() { return $this->model; }
			
		}
		
		?>
	
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.
Putting the classes to use
Once the classes have been created it's just a matter of putting them to use. In a real-world situation we would be tying these classes to a database and creating multiple car objects to use in our online storefront. For the example in this article we include the objects and create a new car instance.
		<?php
		
		include('classes/Product.class.php');
		include('classes/Car.class.php');
		
		$car = new Car("Land Rover", "Range Rover", "50,000.00", "img/07_Range_Rover.jpg", "Unequaled and uncompromised, the Range Rover for 2008 remains the benchmark of luxury and refinement. Even while Range Rover stands as a classic icon, it never stands still. For 2008, Range Rover returns with refinements and the continued innovation of Terrain Response.™");
		
		echo '<img src="'. $car->GetPhoto() .'" align="left" />';
		echo '<b>'. $car->GetName() .'</b>: '. $car->GetModel();
		echo '<p>Starting from: '. $car->GetPrice() .'</p>';
		echo '<p>'. $car->GetDescription() .'</p>';
		
		?>
	
The car instance in this example is a Land Rover, a Range Rover with a price, photo and description. Once all of these properties have been set through the constructor we can call the getters and write out the information to the current document.
About the Author
Kris Hadlock is the owner and founder of Studio Sedition (http://www.studiosedition.com), a Web design and development firm. He is the author of Ajax for Web Application Developers and has been a feature writer for numerous Web sites and design magazines. To learn more about Kris, visit his Web site at www.studiosedition.com or his blog at blog.studiosedition.com
This article originally appeared on WebReference.com.