asadlive84

Encapsulation

May 14th, 2015
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.39 KB | None | 0 0
  1. <?php
  2.  
  3. class Person
  4. {
  5.     protected $name;
  6.     protected $age;
  7.  
  8.     public function __construct($name)
  9.     {
  10.         $this->name=$name;
  11.     }
  12.  
  13.     public function setAge($age)
  14.     {
  15.         if($age<18)
  16.         {
  17.             throw new Exception("Error Processing Request");
  18.            
  19.         }
  20.  
  21.         $this->age=$age;
  22.     }
  23.  
  24.     public function getAge()
  25.     {
  26.         return $this->age;
  27.     }
  28.  
  29.  
  30. }
  31.  
  32.  
  33. $asad=new Person('Asad');
  34. $asad->setAge(20);
  35. //$asad->age=2;
  36. echo $asad->getAge();
Advertisement
Add Comment
Please, Sign In to add comment