What is encapsulation with simple example in php?
What is encapsulation with simple example in php?
Encapsulation is just wrapping some data in an object. The term "encapsulation" is often used interchangeably with "information hiding". Wikipedia has a pretty thorough article.
Here's an example from the first link in a Google search for 'php encapsulation':
<?php
class App {
private static $_user;
public function User( ) {
if( $this->_user == null ) {
$this->_user = new User();
}
return $this->_user;
}
}
class User {
private $_name;
public function __construct() {
$this->_name = "Joseph Crawford Jr.";
}
public function GetName() {
return $this->_name;
}
}
$app = new App();
echo $app->User()->GetName();
?>
Encapsulation is a way of storing an object or data as a property within another object, so that the outer object has full control over what how the internal data or object can be accessed.
For example
class OuterClass
{
private var $innerobject;
function increment()
{
return $this->innerobject->increment();
}
}
You have an extra layer around the object that is encapsulated, which allows the outer object to control how the inner object may be accessed. This, in combination with making the inner object/property private, enables information hiding.
/* 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/
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.
Wrapping up of data inside single unit is called encapsulation. Means class members such as method and properties binds together inside a class to avoid accessibility from outside.This is done by making variables and functions of class private.
Another approach for encapsulation with function (without class) + usage of "use"(optional), to use outer variables inside encapsulation function
<?php
$abc = 'ABC';
// Encapsulation Function
(function () use ($abc){
echo $abc; // prints ABC
$xyz = 'XYZ';
})();
echo $xyz; // warning: undefined