What is encapsulation with simple example in php?
Encapsulation is protection mechanism for your class and data structure. It makes your life much easier. With Encapsulation you have a control to access and set class parameters and methods. You have a control to say which part is visible to outsiders and how one can set your objects parameters.
Access and sett class parameters
(Good Way)
<?php
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;
}
}
Now you can create an object from your User class and you can safely set gender parameters. If you set anything that is wrong for your class then it will throw and exception. You may think it is unnecessary but when your code grows you would love to see a meaningful exception message rather than awkward logical issue in the system with no exception.
$user = new User();
$user->setGender('male');
// An exception will throw and you can not set 'Y' to user gender
$user->setGender('Y');
(Bad Way)
If you do not follow Encapsulation roles then your code would be something like this. Very hard to maintain. Notice that we can set anything to the user gender property.
<?php
class User
{
public $gender;
}
$user = new User();
$user->gender = 'male';
// No exception will throw and you can set 'Y' to user gender however
// eventually you will face some logical issue in your system that is
// very hard to detect
$user->gender = 'Y';
Access class methods
(Good Way)
<?php
class User
{
public function doSomethingComplex()
{
$this->doThis(...);
...
$this->doThat(...);
...
$this->doThisExtra(...);
}
private function doThis(...some Parameters...)
{
...
}
private function doThat(...some Parameters...)
{
...
}
private function doThisExtra(...some Parameters...)
{
...
}
}
We all know that we should not make a function with 200 line of code instead we should break it to some individual function that breaks the code and improve the readability of code. Now with encapsulation you can get these functions to be private it means it is not accessible by outsiders and later when you want to modify a function you would be sooo happy when you see the private keyword.
(Bad Way)
class User
{
public function doSomethingComplex()
{
// do everything here
...
...
...
...
}
}
Encapsulation is the process of hidding the data of the object from outside world and accessed to it is restricted to members of the class.
/* class that covers all ATM related operations */
class ATM {
private $customerId;
private $atmPinNumber;
private $amount;
// Verify ATM card user
public function verifyCustomer($customerId, $atmPinNumber) {
... function body ...
}
// Withdraw Cash function
public function withdrawCash($amount) {
... function body ...
}
// Retrieve mini statement of our account
public function miniStatement() {
... function body ...
}
}
In the above example, we have declared all the ATM class properties (variables) with private access modifiers. It simply means that ATM class properties are not directly accessible to the outer world end-user. So, the outer world end-user cannot change or update them directly.
The only possible way to change a class property (data) is a method (function). That’s why we have declared ATM class methods with public access modifier. The user can pass the required arguments to a class method to do a specific operation.
It means users do not have whole implementation details for ATM class. It’s simply known as data hiding.
Reference: http://www.thecreativedev.com/php-encapsulation-with-simple-example/
Simply I prefer that is visibility of your class's property and method. For example- - public - private - protected
Let's take a look real life example for encapsulation.
class MyClass{
private $name;
public function showName($newName){
$this->name = $newName;
return $this->name;
}
}
//instantiate object
$obj = new MyClass();
echo $obj->showName("tisuchi");
In this case, encapsulation means, we restrict some properties. Like, name property, we can not access from outside of the class. On the other hand, we can access public function entitled showName() with one private parameter.
Simply what I prefer of encapsulation is-
Although, if you have any intension to understand encapsulation further, I refer to my special tutorial based on encapsulation.
http://tisuchi.com/object-oriented-php-part-3-encapsulation-php/
Hope, it will make your concept more clear. Have fun!
The opposite of encapsulation would be something like passing a variable to every method (like a file handle to every file-related method) or global variables.
Encapsulation is the mechanism that binds together code and the data it manipulates, and keeps both safe from outside interference and misuse. The wrapping up of data and methods into a single unit (called class) is known as encapsulation. The benefit of encapsulating is that it performs the task inside without making you worry.