PHP

Reimplementing Methods from My Base Class

One of the things I would like my new DiscountedProduct class to have is an updated constructor that takes in the discount as a parameter. PHP permits us to do this and even to continue to use the base class's own constructor by using a keyword called parent. This keyword, combined with the scope resolution operator (::), lets us call methods belonging to my base class. The act of creating a "new" version of a member function in an inheriting class is often referred to as overriding a method. The overridden method in the base class is replaced by the new one in the extending class.

<?php
  public DiscountedClass extends Product
  {
    protected $discount_percentage = 0.0;
    // etc.
    // one new parameter for the discount.
    public function __construct
   (
      $in_prodid,
      $in_prodname,
      $in_proddesc,
      $in_price_pu,
      $in_location,
      $in_discount_pct
   )
   {
      parent::__construct($in_prodid,
                          $in_prodname,
                          $in_proddesc,
                          $in_price_pu,
                          $in_location);
      $this->discount_percentage = $in_discount_pct;
   }
  }
?>

By doing this, I do not have to duplicate the work done by my parent's constructor. More importantly, I do not have to worry about the Product class's implementation changing and ours divergingwe have taken code reuse to a whole new level.

Note that PHP will now make sure I call the constructor for DiscountedProduct with six parameters, not just the five that the base class requires. I can similarly create a new version of the destructor:m>

<?php
  public DiscountedClass extends Product
  {
    // etc.
    public function__destruct()
    {
      // it is always a good idea to call my parent's destructor
      parent::__destruct();
    }
  }
?>

To make my Discounted Product class work, I'll want to override one more methodthe get_PricePerUnit method:

<?php
  public DiscountedClass extends Product
  {
    // etc.
    public function get_PricePerUnit()
    {
      return parent::get_PricePerUnit()
             * ((100.0 - $this->discount_percentage) / 100.0);
    }
    // etc.
  }
?>


Making Classes Act the Same: Polymorphism

So far, I have shown how to take my little code library for products and turn it into a class, and shown how to encapsulate the properties and operations on a product. However, there is one aspect of my product class that still seems unfortunate: In the implementation of many of the product class's member functions, it still has to perform a comparison check of where the product comes from and then execute separate code paths. Worse, if I want to add new product sources (for example, if I were to team up with a boat manufacturer to start selling boats), I would need to dig into the code and add more cases and code paths to handle the new locations.

Ideally, I would like some way to create a series of classes that have the same public interface (defined as the publicly accessible variables and methods on a class) but have separate implementations that know about the various locations from which products come.

by BrainBellupdated
Advertisement: