PHP

Visibility: Controlling Who Sees Things

One major problem with my Product class is that my member data is publically readable and writable, as indicated by the keyword public in front of the five member variables. What I would really like to find is a way for the object to store the data in such a way that only it can modify the data and otherwise provide some other way for external authors to query their values.

To do this, PHP provides visibility keywords, or keywords that you use to control who can view and modify member variables as well as call member functions. Three visibility modifiers are provided:

  • public Anybody may read and modify this member variable or call this method. This modifier can be used by external code, from code within the given class, or in classes that extend the functionality of the given class .

  • private Only code within this class may read and modify private variables or call private member functions. No other codes, classes, or classes that extend the functionality of this class may use private items.

  • protected External code and other classes that have no relation to the given class may neither read or modify member variables with the protected keyword, nor call protected member functions. However, classes that extend the functionality of this class are allowed to access or call it.

Therefore, for my Product class, if I did not want people to modify the name, PID, or description of a product, I could change the visibility modifier from public to either private or protected. (I will use protected to allow ourselves to extend my class later on.) I have shot ourselves in the foot, however, since external code is now unable to query the values of my member data and learn about the product. ("I have a million products in my database, and I are not going to tell you about any of them!")

To solve this problem, I can create functions to get the values of the member data I wish to expose to people. My Product class will now start to look as follows:

<?php
  class Product
  {
    protected $id;
    protected $name;
    protected $desc;
    protected $price_per_unit;
    protected $location;
    public function __construct
    (
      $in_prodid,
      $in_prodname,
      $in_proddesc,
      $in_price_pu,
      $in_location
    )
    {
      $this->id = $in_prodid;
      $this->name = $in_prodname;
      $this->desc = $in_proddesc;
      $this->price_per_unit = $in_price_pu;
      $this->location = $in_location;
    }
    public function __destruct()
    {
    }
    public function get_number_in_stock($in_num_desired)
    {
      // details omitted
    }
    public function ship_product_units($in_num_shipped)
    {
      // details omitted
    }
    public function get_ProductID()
    {
      return $this->id;
    }
    public function get_Name()
    {
      return $this->name;
    }
    public function get_Description()
    {
      return $this->desc;
    }
    public function get_PricePerUnit()
    {
      return $this->price_per_unit;
    }
    public function get_Location()
    {
      return $this->location;
    }
  }
?>

If I wanted to declare a number of helper functions or subroutines in my class that I could call only from within the implementation of other functions, I would declare them as being private or protected. For example, while implementing my get_number_in_stock_method, I might want to call other member functions, depending on where the products are received:

<?php
  class Product
  {
    // members, constructors, other methods
    public function get_number_in_stock($in_num_desired)
    {
      if ($this->location == LOCAL_PRODUCT)
      {
        return $this->check_local_product_inv($in_num_desired);
      }
      else
      {
        return $this->check_nav_partner_inv($in_num_desired);
      }
    }
    private function check_local_product_inv($in_num_desired)
    {
      // go to local databases and see how many I have
    }
    private function check_nav_partner_inv($in_num_desired)
    {
      // go to navigation equipment partner servers and see
      // how many they claim to have left ...
    }
    // etc...
  }
?>


by BrainBellupdated
Advertisement: