Overview
Encapsulation is one of the four pillars of Object Oriented programming: abstraction, encapsulation, inheritance, and polymorphism.
Encapsulation is also called data hiding because the essence of encapsulation is hiding the internal working of a class. You would do this to hide data and/ or functionality you want to keep private, but also to maintain control over what data gets stored and how it gets stored.
Example
You are processing an online reservation form for the company's New Years party. When a reservation form is submitted, a Reservation object is created.
You want to make sure that the submitted value for the number of people is:
- really an integer number (not text, not a fraction)
- larger than zero
In this class, the $number_of_guests property is private because we want to control what kind of data gets stored in there.
That property is private, but we do want users to be able to provide a value and read the stored value, so we make two public methods available: setNumberOfGuests() and getNumberOfGuests().
setNumberOfGuests validates (ensures it's correct) the provided data before saving it in the $number_of_guests property.
getNumberOfGuests() makes sure a value has been set before attempting to return that value. If no value was set, zero is returned.
Public methods that are used to control private properties like this are called getters and setters.
About this example
More experienced developers will remark that several things in this code example can be improved, including dealing with default values and a better way to handle errors.
We will address these improvements throughout these materials as you learn new techniques and concepts.
Just remember: what you're learning now is certainly not wrong, but can be improved upon.
Private by default?
It's a common approach to make all properties and methods private by default, and to provide public get and set methods for those properties that need to be accessible from the outside.
This approach is considered a good practice for all but the smallest and simplest classes that don't need such level of control.
While you're still learning OOP principles it's not really necessary to make everything private and write getters and setters all the time. However, the sooner you get into the habit of thinking about the accessibility of your properties and methods, the better.
Summary
- Encapsulation is one of the 4 pillars of Object Oriented Programming.
- Encapsulation is a technique where (some of) an object's internal workings are hidden to the end user.
- Internal functionality and data can be hidden by making (some) properties and methods private, while providing public methods to access them.
- Public methods that control private properties are often called getters and setters.
- It's a good practice to always hide the properties and methods that should not be visible or used externally. Keep everything private by default except the things you really want to expose to the end users.