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
<?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";
?>
Related Searches to PHP Encapsulation - PHP OOPs Encapsulation
php encapsulationexplain encapsulatin in phpphp oops encapsulationbenefits of encapsulation in phpphp oop class encapsulationwhat is encapsulation in phpencapsulation in phpabstraction and encapsulation in phpabstraction in phpinheritance in php