Difference between Abstraction and Encapsulation in PHP

Abstraction and Encapsulation in PHP

Difference between Abstraction and Encapsulation

Abstraction

1. Abstraction solves the problem in the design level.
2. Abstraction is used for hiding the unwanted data and giving relevant data.
3. Abstraction lets you focus on what the object does instead of how it does it
4. Abstraction- Outer layout, used in terms of design.
For Example:-
Outer Look of a Mobile Phone, like it has a display screen and keypad buttons to dial a number.

Encapsulation

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- Inner layout, used in terms of implementation.
For Example:- Inner Implementation detail of a Mobile Phone, how keypad button and Display Screen are connect with each other using circuits.

The easier way to understand Data Abstraction and data encapsulation is as follows:-

to understand Abstraction and Encapsulation Take an example of Mobile Phone:-

You have a Mobile Phone, you can dial a number using keypad buttons. Even you don’t know how these are working internally. This is called Abstraction. You have the only information that is needed to dial a number. But not its internal working of mobile.
But how the Mobile Phone internally working?, how keypad buttons are connected with internal circuit? is called Encapsulation.

Abstraction and Encapsulation in PHP In summary, while abstraction focuses on defining the essential characteristics and behaviors of objects, encapsulation focuses on bundling data and methods together and controlling access to them. Both concepts are fundamental to creating well-structured, modular, and maintainable PHP code.

Scroll to Top