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.
php-encapsulation

Sample Code

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

Output

welcome Kaashiv
You are 22 years old


Related Searches to PHP Encapsulation - PHP OOPs Encapsulation