Encapsulation in PHP

Encapsulation in PHP
  • Encapsulation is a concept where we encapsulate all the data and member functions together to form an object.
  • Wrapping up data member and method together into a single unit is called Encapsulation.
  • Encapsulation also allows a class to change its internal implementation without hurting the overall functioning of the system.
  • Binding the data with the code that manipulates it.
  • It keeps the data and the code safe from external interference.

Example 1

snippet
<?php
class person
{
public $name;
public $age;
function __construct($n, $a)
{
$this->name=$n;
$this->age=$a;
}
public function setAge($ag)
 {
 
$this->ag=$ag;
 
}
 
public function display()
 
{
 
echo  "welcome ".$this->name."<br/>";
 
return $this->age-$this->ag;
 
}
 
}
 
$person=new person("sonoo",28);
 
$person->setAge(1);
 
echo "You are ".$person->display()." years old";
 
?>

Output:

Encapsulation in PHP
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +