Introduction to PHP – Object Oriented Programming Part 6

By this time you are now capable of creating classes, inheritance and just about the basics of Object Oriented Programming. This time around, we are going to deal with the visibility of methods and properties. Beginning with PHP 5 there are visibility that you can set with your methods and properties. In the previous tutorial you already seen that some of the variables that have been declared as private and public. We are going to learn all about these in this tutorial.

There are three types of visibility in PHP namely public, private and protected.

Public
The public visibility will set any properties and methods to be visible anywhere. Hence, the word 'public', meaning it can be accessed anywhere or no class restrictions. In PHP 5, methods and variables that don't have any visibility are always default as public.

Private
The private visibility will set any properties and methods as private and can only be accessed within the class itself. Think of its a private property or land. In order for you to access that land you need to have proper clearance. But for the owner itself, they can freely do whatever they want on their land.

Protected
The protected visibility will set any property or methods as protected in which they can only be accessible with the class that declared it and the class that inherit the above declared class.