Encapsulation in PHP

Encapsulation in PHP

Encapsulation in PHP programming means we restrict some properties. It typically involves Data hiding, and it’s a technique to hide some attributes or operations of the class from the outside world.

There are three types of scope resolution operators which are used to define the scope of an object, attribute or function.

  • Public
  • Private
  • Protected



The quality which sets the scope to public or which can be accessed throughout the scope of the project. Public keyword is used to set the access of the specific member of the class to public. When this access specifier is assigned the members are visible everywhere in the application and we can simply access them by creating the object of the class, in which they are declared.


The private attributes of the class are only visible inside that class. Private attributes are not accessed even in the child class. They are private members and are only visible inside the class they are declared in, and only operations and functions of that class can access and modify it.



It’s a special type of scope resolution operator which is actually private but public for only those classes which are inherited from that class.

Before we understand the concept of encapsulation we have to understand the inheritance in PHP because it’s a prerace of inheritance as

we are dealing with inherited classes while we have protected attributes.

Inheritance in PHP

As we already discussed inheritance in other languages, it is a bit different in C#, but it’s same in PHP. In inheritance a sub class extends from a super class and able to use all of its functions and attribute which are public or either protected but not private.

PHP support following types of inheritance:

  • Single level inheritance
  • Multilevel inheritance
  • Hybrid inheritance
  • Multiple inheritance

PHP also use extends keyword to inherit any class same like in Java.