PHP Encapsulation - PHP OOPs Encapsulation

PHP Encapsulation

  • Encapsulation in PHP is a process of combining data and functions that work on that data into a single unit called a class.
  • This helps to protect the data from outside interference and misuse, and also helps to keep the code within the class more organized.
  • In encapsulation, the data and functions are bound together within a class, making the data inaccessible to anyone outside the class.
  • Encapsulation also promotes the concept of data hiding, which means that the data within the class is not visible to the outside world.

Sample Code

class man  
public $name;  
public $age;  
function __construct($n, $a)  
public function setAge($ag)  
public function show()    
echo  "welcome ".$this->name."<br/>";    
return $this->age-$this->ag;     
$man=new man("Kaashiv",23);   
echo "You are ".$man->show()." years old";     


welcome Kaashiv
You are 22 years old

Related Searches to PHP Encapsulation - PHP OOPs Encapsulation