[Previous] [Contents] [Next]


One of the powerful concepts in object-oriented programming is inheritance. Inheritance allows a new class to be defined by extending the capabilities of an existing base class. PHP allows a new class to be created by extending an existing class with the extends keyword. Example 2-9 shows how the class Counter is extended to create the new class BottleCounter that can determine the number of cases of wine to be shipped.

Example 2-9. A new class BottleCounter is defined by extending the base class Counter
    "-//W3C//DTD HTML 4.0 Transitional//EN"
   <head><title>Bottle Counter</title></head>
    include "counter.inc";
    class BottleCounter extends Counter
     // Add 12 bottles to the counter
     function addCase(  )
      $this->count += 12;
     // Return the number of cases to be shipped
     function caseCount(  )
      return ceil($this->count / 12);
     // A Constructor that sets the initial count
     function BottleCounter($startCount)
      $this->count = $startCount;
    // Create a new object of type "BottleCounter"
    // and pass the initial count of 12
    $temp = new BottleCounter(12);
    // Increment the counter
    $temp->increment(  );
    // Add another Case
    $temp->addCase(  );
    // Print out the value of the counter: 24
    echo "<p>Counter is now: ";
    $temp->showvalue(  );
    // Print the number of cases
    $cases = $temp->caseCount(  );
    echo "<p>The number of cases to ship: $cases";

The new class BottleCounter doesn't add any new member variables but does add three new member functions. The functions of the class BottleCounter use the member variables of the base class Counter in ways appropriate to BottleCounter. The function addCase( ) increments the $count variable by 12, and the function caseCount( ) returns the total number of cases that need to be shipped, including any partially filled cases.

The final function, BottleCounter( ), is the constructor of the class BottleCounter. Member functions with the same name as the class are treated differently. PHP uses these functions as constructors, and they are called when new objects of that class type are created. A constructor function can include arguments that can be used to initialize member variables when a new object is created. Example 2-9 showed how a new BottleCounter object is created:

// Create a new object of type "BottleCounter"
// and pass the initial count of 12
$temp = new BottleCounter(12);

The power of inheritance doesn't come from simply reusing code. Objects created from the extended class can be used as if they were created from the existing base class. This ability to use an object as if it were an instance of the base class is known as polymorphism. You can use the class Counter as a base for other new classes, such as a CanCounter class in which a case is 24 cans, not 12 bottles. Code that uses an object of class Counter can then be used with objects of type BottleCounter or CanCounter. Consider this example, which defines the function volumeDiscount( ), designed to return a discount factor based on a Counter object:

// Return a discount factor based on
// the value of the Counter $var
function volumeDiscount($var)
  // use $var as a Counter
  if ($var->count > 24)
    return 0.95;
    return 1.0;
$bottles = new BottleCounter(10);
$cans = new CanCounter(24);
$bottleDiscountFactor = volumeDiscount($bottles);
$canDiscountFactor = volumeDiscount($cans);

If both the BottleCounter and CanCounter classes are defined as extensions of Counter, the function volumeDiscount( ) can be called on objects of those classes.

[Previous] [Contents] [Next]