OOP PHP Lesson 2 – Simple Class Interface

featured image for php oop lesson 2 - basics

Second lesson of object oriented programming in PHP language will introduce access specifier private. Lets modify Customer class (Customer.php):

class Customer {

private $name=""; //set as empty string

public function set_name($a_name)
{
   if($this->name=="") {
        $this->name = $a_name;
     }

else {
        echo 'Customer is already named: '.$this->name.'<br />';
     }
}

public function get_name()
{
   echo $this->name.'&lt;br /&gt;';
}

public function say_hello()
{
   if($this->name!="") {
          echo 'Customer '.$this->name.' says: Hello!<br />';
     }

else {
          echo 'Please give name to your new customer (there is nobody to say Hello!)<br />';
     }
}

}

As we can see $name property is now private, which means that we are unable to get directly its value after object creation. However, in definition of Customer class there are three methods – public functions – which are interface of the class, and all of them have access to $name ($this->name). Methods of a class have access to all properties (and other methods) of this class, even if the properties are set as private.

Methods set_name and get_name are very simple, but in spite of that they play very serious role. Let’s assume, that some class properties can have only limited set of values, so letting them to be set via direct access from user panel can make some problems. It is better to write methods which make setting the properties’s values more safe. For instance set_name method makes it possible to give name like “u67t”, which as a human name doesn’t make sense. Reader of this article can modify the method, as an exercise, so only letters in range a-z can be used for setting customer name.

The code for checking how our class is working is below:

require('Customer.php');

$a_customer = new Customer();
$a_customer->set_name("Steven"); //
$a_customer->get_name();
$a_customer->set_name("John"); //we can try, but it is impossible to change name
$a_customer->get_name(); //still the previous name
$a_customer->say_hello();
echo $a_customer->name; //no way to get direct access to $name

We can check, that $a_customer->name gives no result (or error), because $name property is private, so we (that is: user) have no direct access to it and can not check it’s value. The value of $a_customer->name can be get only via interface method get_name. Class interface stands between user and class sensitive methods and properties. A rule of thumb is: don’t give direct access to class methods and properties, if this access is not really needed. For greater safety use class interface. In the above example we set name Steven, and after that tried to change it to John, but it was impossible. Class interface (public methods) restricts access to $name using logic implemented in class interface method set_name. You can modify set_name method as you wish – users can not change it. Once object is created, its behaviour is fixed as designed in class definition. We could ask: is it really needed to limit access to some properties via private? Yes, it is needed, for safety reasons. There are always some people which try to spoil something only for the very reason that it is possible to spoil it.