Ibrahim Azhar Armar
Ibrahim Azhar Armar

Reputation: 25755

What is the use of encapsulating data in case of PHP?

I Often ask this question to myself again and again. what is the use of encapsulating the data in PHP. although i do understand it's usage and usefulness of hiding the data or design implementation of an application from Public. i do agree this way i could achieve lots of security by not allowing other developers to see how i implemented other methods. now in case of PHP i still have some unanswered questions.

a) How does encapsulating data in PHP gonna help me because as PHP does not provide any encryption system (i know there exist few but i am talking in general) which will encrypt my source code from being read by others, anyone can always comeback see my source and modify accordingly breaking the rules. now this lays down the whole purpose of data encapsulation isn't it?

Now there may be different situation i may have to handle using PHP let me take into consideration two scenarios where i have two deal according to the situation.

SITUATION A :

A Clients wants me to develop a Web Application, Which will be stored locally into his desktop machine and accessed from there. in the context of this situation i can do nothing to make sure no one touches the source code. Encapsulating the data is of no use here i see. isn't it?

Situation B:

I need to develop a Web Application which will be hosted in server and accessed from there, and i have defined several class which is bounded by encapsulation, but i want other developers to extend the class by making use of Public API. i really do not have any idea on how i can allow anyone(developers) to extend using my class in this situation? i am really not sure on how public API things work?? i will be grateful if someone could put some light on the logic behind implementing PUBLIC API. and is this where Data Encapsulation comes into picture? is this the only situation where i will badly need encapsulation to be implemented within my application?

Thank you.

Upvotes: 1

Views: 492

Answers (2)

Palantir
Palantir

Reputation: 24182

I believe you are mixing conepts here. One thing is the design principle of data encapsulation, another concept is the availability of the source code or reverse engineering.

Data encapsulation is important to your development team. Its advantages are the fact that code is more reusable thanks to the clear interfaces and the code is more easy to read and understand because data are logically separated.

This has nothing to do with code availability and possibility to reverse engineer your code. Even if data are encapsulated, your code can still be viewed and analyzed.

Those concepts meet when you are publishing a closed source library. You want to make sure that it has a clear public API, provided as an interface, and you want to make sure it is encrypted so nobody can copy its internals.

Upvotes: 1

Gordon
Gordon

Reputation: 317119

You do not use Encapsulation because it prevents people from touching your code but because you want to create a limited but dedicated public API for your objects to exchange messages through. This a key element in in OOP paradigm. The aim is not to security but maintainable and flexible applications. If people want to modify your code, let them. They are grown ups.

Upvotes: 2

Related Questions