- passive income

What are Encapsulation and Abstraction In PHP

What is Object Oriented Programming

The Object Oriented Programming (OOP) is a programming model where programs are organized around object and data rather than action and logic. Object Oriented Programming (OOP) allow decomposition of a problem into a number of entities called object and then builds data and function around these objects.

  • 1. The program is divided into number of small units called object. The data and function are build around these objects.
  • 2. The data of the objects can be accessed only by the functions associated with that object.
  • 3. The functions of one object can access the functions of other object.


All the programming languages supporting Object Oriented Programming (OOP) will be supporting these four main concepts:

  • 1. Encapsulation
  • 2. Inheritance
  • 3. Polymorphism
  • 4. Data abstraction

What is encapsulation

Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. You can use encapsulation if the properties of an object are private, and the only way to update them is through public methods. For example in this code we can protect our User objects from setting an invalid gender parameter.

class User {

 private $gender;

 public function getGender() {
  return $this->gender;

 public function setGender($gender) {
  if ('male' !== $gender and 'female' !== $gender) {
   throw new Exception('Set male or female for gender');
  $this->gender = $gender;
  return $this;

$user = new User();

In above example we should assign male or female value for setGender method or it will print

Fatal error: Uncaught exception 'Exception' with message 'Set male or female for gender'.

Note again : Encapsulation means hiding the internal details of an object, i.e. how an object does something. Encapsulation prevents clients from seeing its inside view, where the behaviour of the abstraction is implemented .  Encapsulation is a technique used to protect the information in an object from the other object.

Real life example of Encapsulation

Let’s take example of Mobile Phone and Mobile Phone Manufacturer
Suppose you are a Mobile Phone Manufacturer and you designed and developed a Mobile Phone design(class), now by using machinery you are manufacturing a Mobile Phone(object) for selling, when you sell your Mobile Phone the user only learn how to use the Mobile Phone but not that how this Mobile Phone works.

Learn , destructor and polymorphism  

What is abstraction 

Data Abstraction is the most important features of any oop's programming language, It show only useful information remaining are hide for end user. Any representation of data in which the implementation details are hidden (abstracted). Abstraction focus only what the object does instead of how.

Consider a very simple example in mathematics world - mulitplication. If we have two variables 'x' and 'y' then we simply say their multiplication without knowing what happening behind the scenes it means how variable value are stored in memory, how variable value convert in binary format, how processor processes the multiplication information.

Let’s see an example

 abstract class MobilePhone
      public Calling(); // calling function
      public SendSMS(); // calling function

 public class Nokia1400 extends MobilePhone
     // code here for Nokia 1400 class

 public class Nokia2700 extends MobilePhone
        public FMRadio(); // calling function
        public MP3(); // calling function
        public Camera(); // calling function

 public class BlackBerry extends MobilePhone
       public FMRadio(); // calling function
       public MP3(); // calling function
       public Camera(); // calling function
       public Recording(); // calling function
       public ReadAndSendEmails(); // calling function

Abstraction means putting all the variables and methods in a class which are necessary. For example: – Abstract class and abstract method. Learn briefly about abstract class .

Difference between Abstraction and Encapsulation


  • 1: Abstraction is the concept of hiding irrelevant details. In other words make complex system simple by hiding the unnecessary detail from the user.
  • 2: Abstraction solves the problem in the design level.
  • 3: Abstraction lets you focus on what the object does instead of how it does it
  • 4: Abstraction solves the problem in the design level. For Example:-
    Look of a Mobile Phone, like it has a display screen and keypad buttons to dial a number.



  • 1: Encapsulation solves the problem in the implementation level.
  • 2: Encapsulation means hiding the code and data into a single unit to protect the data from outside world.
  • 3: Encapsulation means hiding the internal details or mechanics of how an object does something.
  • 4: Encapsulation solves the problem in the implementation level.
    For Example:- Implementation detail of a Mobile Phone, how keypad button and Display Screen are connect with each other using circuits.

Leave a comments


Recent Tweets