As applied to PHP, class inheritance is accomplished by using the extends keyword. Listing 7-3 demonstrates this ability, first creating anEmployeeclass, and then creating anExecutive class that inherits fromEmployee.
¦Note A class that inherits from another class is known as a child class, or a subclass. The class from which the child class inherits is known as the parent, or base class.
Listing 7-3. Inheriting from a Base Class
<?php # Define a base Employee class class Employee {
private $name;
# Define a setter for the private $name member. function setName($name) { if ($name == "") echo "Name cannot be blank!"; else $this->name = $name; }
# Define a getter for the private $name member function getName() { return "My name is ".$this->name."<br />"; } } #end Employee class
# Define an Executive class that inherits from Employee class Executive extends Employee { # Define a method unique to Employee function pillageCompany() { echo "I'm selling company assets to finance my yacht!"; } } #end Executive class
# Create a new Executive object $exec = new Executive();
# Call the setName() method, defined in the Employee class $exec->setName("Richard");
# Call the getName() method echo $exec->getName();
# Call the pillageCompany() method $exec->pillageCompany(); ?>
This returns the following:
-------------------------------------------- My name is Richard. I'm selling company assets to finance my yacht! --------------------------------------------
Because all employees have a name, theExecutiveclass inherits from theEmployeeclass, saving you the hassle of having to re-create thename member and the corresponding getter and setter. You can then focus solely on those characteristics that are specific to an executive, in this case a method namedpillageCompany(). This method is available solely to objects of typeExecutive, and not to theEmployeeclass or any other class, unless of course we create a class that inherits fromExecutive. The following example demonstrates that concept, producing a class titledCEO, which inherits fromExecutive:
<?php
class Employee { ... }
class Executive extends Employee { ... }
class CEO extends Executive { function getFacelift() { echo "nip nip tuck tuck"; } }
$ceo = new CEO(); $ceo->setName("Bernie"); $ceo->pillageCompany(); $ceo->getFacelift();
?>
BecauseExecutivehas inherited fromEmployee, objects of typeCEOalso have all the members and methods that are available toExecutive.
Please check back next week for the second part of this series.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.