encapsulation in php

Solutions on MaxInterview for encapsulation in php by the best coders in the world

showing results for - "encapsulation in php"
Joachim
09 Jun 2016
1<?php
2/**
3 * we can retrive value outside of class as well as we can change it, so we need to  prevent it by using encapsulation
4 * constructor inside variable default access is public.
5 * after doing variable to protected you neither change their value nor access their value
6 * using getter and setter method you can get and set the value of protected variable.
7 * 
8 * 
9 * 3 types of access modifier
10 * 1) public: we can use value of variable inside and outside the class itself
11 * 2) protected: we can use value of variable only inside the class, not outside the class, can use in child class.
12 * 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.
13 */
14class Birds
15{
16    protected $num;
17    function __construct()
18    {
19        $this->num = 3;
20    }
21}
22class Parrot extends Birds
23{
24    function getNum()
25    {
26        return $this->num;
27    }
28}
29$obj = new Parrot();
30
31// $obj -> num = 7;        //need to prevent it.
32echo $obj->getNum();
33?>