0

For instance i have class say that runs MySQL query and i want to encapsulate this class.

class SqlQuery { private $database_connection_obj; public function __construct($dbh) { $this->$database_connection_obj = $dbh; } public function runQuery($query, $params) { //run query implementation... } } 

Encapsulation and information hiding means that i want to close direct access to class methods so function runQuery() should not be public so i make it private and add method exacuteQuery() with sole purpose to pass data to private function runQuery().

What is the practical use of doing so, because at the end it works exact same as code above. Should there be some sanitation of input data done in public method before its passed to private method or why write this extra code at all?

class SqlQuery { private $database_connection_obj; public function __construct($dbh) { $this->$database_connection_obj = $dbh; } public function exacuteQuery($external_query, $external_params) { $this->runQuery($external_query, $external_params); } private function runQuery($query, $params) { //run query implementation... } } 
2
  • I don't see any reason to wrap runQuery() in the given code. Why do you think you need to complicate your code with such kind of wrapper? =) Commented Nov 2, 2016 at 17:28
  • Whats encapsulation then can you give me a example. Commented Nov 2, 2016 at 17:31

2 Answers 2

2

The main idea is to make classes look like black boxes, meaning that an outsider (namely, another class or function holding an instance of the class) has no idea how it works internally. It's abstract to them.

The internal workings of the query are of no importance to the class holding the query and calling ->run(), it only cares about the query running, not necessarily how or where it runs.

Note that a query specifically is quite the low level abstraction, it's relatively close to making direct calls to standard library functions/objects. But given that low level abstraction, you can make higher level abstraction.

For example, a UserStore, that internally uses SqlQuery objects to get and set User objects into the database. A class using UserStore isn't aware that UserStore is using SqlQuery objects internally, it just knows that it's an object that can save and retrieve User objects.

This sort of "hiding away" and "encapsulating" actually gives you a lot of power, because now classes can use other classes without depending on specific implementation details. If tomorrow you'd like to change the way you store Users, you just make a change to the UserStore class, as long as it has the same public API, the rest of your application wouldn't even be aware that something changed.

Sign up to request clarification or add additional context in comments.

2 Comments

I got the theory, my question is more about implementation in PHP would you say first or second code example i provided should be used? why.
Oh, apologies. Specifically, there's no benefit to making a public function that only delegates to a private function with the same signature. You can use delegation to private functions to abstract away similar functionality from multiple methods (for example, a common piece of functionality both select() and update() my have is the need to prepare the query, that can be a private function that both publics call, to get some service from.
1

Given details has no difference, but it depends upon work. I use it much times when i have to make a function which cannot be access directly but only by the method for its use. It optimize the code and deny access outside the code. Otherwise any one can alter the parameters which can be harmful.

2 Comments

Can you provide example please.
i often use it for emails, a private method that only method after certain conditions can send only by other method. It is an example

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.