free tutorials logo spacer
spacer
PHP
 
     
   
shaddow bottom left

All PHP code is in RED

Inheritance, Extending the vehicle example 2

So far, we have made a vehicle class and decided to instantiate cars, trucks, lories, and any other vehicle directly from it, here we will add 2 classes, car and truck that share everything in vehicle and still have methods and properties of there own, you can copy and paste this into any php document and play with it a bit until you are familiar with the basics of OO.

An explanation of the new things in this code will follow, Part by part, so don't worry, be happy.

<?php
class vehicle
{

     public $price = "25000";
     public $manufacturer = "Toyota";
     public $engineSize = "2 Liter";
     public $color = "White";
     
     function getspecs()
     {
       return "Vhicle Manufacturer: {$this->manufacturer} \n<br>
               Engine Size: {$this->engineSize} \n<br>
               Color: {$this->color}\n<br>
               Price: {$this->price}  ";
     }
      
     function __construct($price, $manufacturer, $engine, $color)
     {
        $this->price = $price;
        $this->manufacturer = $manufacturer;
        $this->engineSize = $engine;
        $this->color = $color;
     }

}


class car extends vehicle
{
	public $numberOfPassengers = 5;
	function __construct($price, $manufacturer, $engine, $color, $numberOfPassengers)
	{
		$this->numberOfPassengers = $numberOfPassengers;
		parent::__construct($price, $manufacturer, $engine, $color);
	}
	function getspecs()
	{
		//When the parent class has a method of the same name, we override it with this method
		//But we can access the parent classes getspecs method from here by using the scope operator
		$specs = parent::getspecs();
		$specs .= "\n<br>
		Number Of passengers: {$this->numberOfPassengers}  ";
		return $specs;
	}
}


class pickup extends vehicle
{
	public $towcapacity = 10000;
	
	function __construct($price, $manufacturer, $engine, $color, $towcapacity)
	{
		$this->towcapacity = $towcapacity;
		parent::__construct($price, $manufacturer, $engine, $color);
		
	}
	
	function getspecs()
	{
		//When the parent class has a method of the same name, we override it with this method
		//But we can access the parent classes getspecs method from here by using the scope operator
		$specs = parent::getspecs();
		$specs .= "\n<br>
		Maximum tow load: {$this->towCapacity}  ";
		return $specs;
	}
}


$hiace = new pickup(25000, "Toyota", "3500 Benzin", "White", 10000);

$camry = new car(31000, "Toyota", "2.5L Benzin", "red", 5);

print $camry->getspecs();

print "<br>-----<br>";

print $hiace->getspecs();

?>

The output to the above code will be

  Vhicle Manufacturer: Toyota
Engine Size: 2.5L Benzin
Color: red
Price: 31000
Number Of passengers: 5
-----
Vhicle Manufacturer: Toyota
Engine Size: 3500 Benzin
Color: White
Price: 25000
Maximum tow load:

As you might notice above, there has been no change to vehicle, we added 2 other classes, one car and one pickup so we can instantiate objects from them rather than from vehicle for both hiace and camry.

Unlike vehicle, the new classes are declared with

class car extends vehicle
and
class pickup extends vehicle

extends is the keyword we use to make a new class inherit a parent class, The child subclass can be seen as the parent class even if it was empty, and the more important thing is that it can add to it or override it.

So, let us take one of the 2 subclasses since they are more or less the same, let us investigate the car subclass

class car extends vehicle

This one simply means that car is a subclass of vehicle

public $numberOfPassengers = 5;

This one is an added property specific to the car class.

function __construct($price, $manufacturer, $engine, $color, $numberOfPassengers)

This is a method, but if you notice it is a constructor for the class CAR, So do we have to rewrite the parent constructor ? The short answer is YES, it will override the parent __construct, but fear not, Since although the parent class constructor is overridden, the overriding function can call the parent's constructor function and pick up from there

First, The new __construct initialized a property
$this->numberOfPassengers = $numberOfPassengers;

Then it called upon the parent constructor to initialize all other properties, pay special attention to the parent keyword and the scope opperator ::, they mean we are exiting the scope of this class one level up to access things back in the parent class.
parent::__construct($price, $manufacturer, $engine, $color);

}

Another overridden method is the getspecs() method.
function getspecs()

Again, it calls the parent with the parent keyword and scope opperator, Gets the values from the getspecs() in the parent and picks up where the parent left off, Now the return value from the parent egtspecs function is in $specs
$specs = parent::getspecs();

Now, the child's getspecs() adds its own touch to the originalk by appending (Concattinating) the number of passengers this car can take

$specs .= "\n<br> Number Of passengers: {$this->numberOfPassengers} "; return $specs; }

And finally, it returns the result
return $specs;

So, in inheritance, we learned to use extend to make a subclass, then we learned to ovverride methods and properties just by using the same name in the child class, and finally, we learned to access the parent's overridden method with the parent keyword and scope opperator.

There you have it, Inheritance 101.

NEXT: 06 Public, Private and protected, Access control, Who can use what



Your Name:

Subject:

Your Comment: