Object Oriented Programming In PHP Part Two – Methods And Visibility 2

In part one of Object Oriented Programming in PHP series I introduced fundamental terms of OOP (class, object and property). Today I will continue with explaining visibility keywords and methods.

The methods differ from the functions in that they are declared in the class.

public function myMethod() {
 
}

They support different types of visibility just like properties do. Visibility keywords are: public, protected and private. These keywords define from where we can access the method. Public methods can be accessed from anywhere while private methods can only be accessed in the same class they are defined in. There is also protected keyword, but we will take a closer look at it a bit later. First, let’s just take a look at the example of using public and private keywords.

class MilitaryBaracks {
 
  //properties that we can access anywhere
  public $name = 'Mougli';
  public $weapon = 'rifle';
 
  //properties that we can access in this class only
  //in practice we usually prepend _ to the private methods for the clarity
  private $_ammunition = 10;
 
  //because we are inside the class, which defines property $_ammunition, we can access that property
  public function getAmmunition() {
    return $this->_ammunition;
  }
 
  public function shoot() {
    //keyword this means that we refer to property of an instance of class -> an object
    //if soldier has any ammunition left, shoot and subtract one bullet
    if($this->_ammunition > 0) {
      $this->_ammunition = $this->_ammunition - 1;
       
      return true;
    }
    return false;
  }
}

Let’s take a look at the above code in more detail. I’ve added a private property $_ammunition and methods getAmmunition() and shoot(). Because we cannot access a private property, we need to create a public method, which returns its value (getAmmunition()). Method shoot() reduces a number of bullets left each time it is called.

It is a common practice to prepend _ to the private properties. This helps to quickly differentiate private properties from public. Maybe all this is confusing, so let me clear it up a bit with an example. Let’s see what happens if I try to change and print out both properties.

// we instantiate an object of type MilitaryBaracks
$soldier = new MilitaryBaracks();
 
// and print out its name (its property)
echo $soldier->name;
echo "<br>";
// we change its name to "Matic"
$soldier->name = 'Matic';
echo $soldier->name;
echo "<br>";
echo $soldier->_ammunition;

This is what we get:

Mougli

Matic

Fatal error: Cannot access private property MilitaryBaracks::$_ammunition in C:\Programi\Wamp\www\script.php on line 42

As you see we can access and modify public properties values just like variables, but we cannot do that with private ones. This is a mechanism to protect properties which values shouldn’t be changed from outside the class. But sometimes (actually quite often) we want to access the private properties from outside of class scope. In that case we create a public method like we did in our case. That way we can enforce the rules – what someone can do with that property. In our case we want to allow just reading so we created a getAmmunition() method.

$soldier = new MilitaryBaracks();
echo "Soldier has " . $soldier->getAmmunition() . " bullets.";
echo "Soldier shoots.";
$soldier->shoot();
echo "Soldier has " . $soldier->getAmmunition() . " bullets.";
Soldier has 10 bullets.
Soldier shoots.
Soldier has 9 bullets.

What does keyword $this means? With the use of $this keyword a class knows it’s operating on this object. For simplicity try changing word $this with “current object instance”.

When I was learning about OOP I was asking myself why would I set a method or property to private, if that just makes things more complicated. You have to write more code, because you have to write an additional method to access the property. Here is the answer. The thing with OOP is that we want to limit the access to class properties and methods as much as possible. This way me try to make code more clear and harder to use in a wrong way.

That is not so obvious in my case, because the example is to simple. But I hope it will become more clear in an upcoming posts, where we will roll up our sleeves and get to some more serious programming ;) .

In next post of this series we will cover one of the most important but also more complex topic of object oriented programming, that is inheritance.

2 thoughts on “Object Oriented Programming In PHP Part Two – Methods And Visibility

  1. Pingback: Object Oriented Programming In PHP Part One – Basics ← Bugs On Steroids

  2. Pingback: Object Oriented Programming In PHP Part Three – Constructor & Destructor ← Bugs On Steroids

Leave a Reply