Encapsulation may seem like an intimidating topic, but it’s actually quite simple. Object oriented programming is based on 3 principles which are encapsulation, inheritance, and polymorphism. In this tutorial, we’ll be talking about encapsulation and why it’s important to understand this principle. We’ll talk about the other 2 in future tutorials.
What is encapsulation?
So, what is the meaning behind this complex word? In basic terms, it’s the way we define the visibility of our properties and methods. When you’re creating classes, you have to ask yourself what properties and methods can be accessed outside of the class. Let’s say we had a property named foo. If a class extends your class, is it allowed to manipulate and access foo? What if someone creates an instances of your class? Are they allowed to manipulate and access foo?
Let’s open up our CanadianPet.class.php file and update our class to this.
class CanadianPet extends Pet
public function setPetName( $newName )
$this->petName = $this->checkPetName( $newName );
private function checkPetName( $petNameToCheck )
return str_replace( 'a', '', $petNameToCheck );
Instead of using str_replace() inside the setPetName() function, we create a new function to check the pet name and returns the result. You’ll also notice we introduce a new keyword called private for our function checkPetName().
Private is a keyword reserved by PHP. This tells PHP that we don’t want anyone accessing this method or property outside of the class. Classes that extend this class can’t use it either. This property or method is exclusively for our CanadianPet class. To give you an example of what would happen if we tried to use this method outside of this class, let’s go to our index.php file and update the code to this.
include( 'classes/pet.class.php' );
include( 'classes/CanadianPet.class.php' );
$jack = new CanadianPet();
echo $jack->checkPetName( 'Jack' );
We directly try to access this method from our instance and if you reload the page, you should see an error. This is the power of encapsulation. We can make our properties and methods private and no one will be allowed to modify or access this method outside of the class.
However, if we tried accessing the setPetName() method, then we would indirectly be using the checkPetName() method which is allowed. This is because we’re not directly using the method, but the method setPetName() is and so it is allowed.
Our keyword Public allows us to access and manipulate methods and properties outside the class or classes that extend it.
There’s one more visibility keyword. In some cases, we don’t want users to access our properties and methods outside of the class, but we may want to allow classes that extend our class to use it.
Open up your pet.class.php file and change the visibility of our $petName property to this.
The Protected keyword allows our properties and methods to be accessed only by the author class or classes that extend it. We can’t, however, access or manipulate it through an instance. This is not allowed.
To sum it all up, encapsulation is just a way to define how our properties and methods can be used. The main reason you may want to make your properties or methods private is because it may mess up the flow of your code. It’s totally up to you. For most cases, you shouldn’t allow users to change properties directly, but allow methods to change and retrieve properties. More information about PHP oop can be found here.