encapsulation in php
<?php
/**
* we can retrive value outside of class as well as we can change it, so we need to prevent it by using encapsulation
* constructor inside variable default access is public.
* after doing variable to protected you neither change their value nor access their value
* using getter and setter method you can get and set the value of protected variable.
*
*
* 3 types of access modifier
* 1) public: we can use value of variable inside and outside the class itself
* 2) protected: we can use value of variable only inside the class, not outside the class, can use in child class.
* 3) public: we can use vlaue of variable inside not outside the class, also we can't use it in inheritance for extend property of child class.
*/
class Birds
{
protected $num;
function __construct()
{
$this->num = 3;
}
}
class Parrot extends Birds
{
function getNum()
{
return $this->num;
}
}
$obj = new Parrot();
// $obj -> num = 7; //need to prevent it.
echo $obj->getNum();
?>