Tuesday , 18 June 2019
Home / Programming / PHP / Encapsulation – PHP OOP Tutorial

Encapsulation – PHP OOP Tutorial

This tutorial is a part of the PHP OOP tutorial series. If you would like to view the complete list of tutorials under this series and the order you should follow, then click here. http://jaskokoyn.com/php-oop-tutorial-series/ 

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.

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.

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.


About Jasko Koyn

Check Also

Abstract Classes

Abstract Classes – PHP OOP Tutorial

Today, we’ll be discussing what abstract classes are and how we could use them. There …


  1. Awesome tutorial. Thanks now i understood the concept of encapsulation . I knew its information hiding but today i understood what exactly is encapsulation

  2. easy and nice explained

  3. fantastic tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.