What is encapsulation with simple example in php?
Encapsulation is just how you wanted your objects/methods or properties/variables to be visible in your application. for example, :
class ecap {
public $name;
private $id;
protected $tax;
}
If you want to access private or protected properties, then you have to use getter and setter methods in your class that will be accessible from outside of your class. Meaning, you cannot access your private or protected properties directly from outside of your class but you can use through any methods. Let’s take a look-
in the class, add the following method:
class ecap
{
public function userId(){
return $this->id;
}
}
and we can access it like:
$obj = new ecap();
echo $obj->userId();