vladislavkopilov

8-1

Nov 16th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. <?
  2.     class User {
  3.         public static $cont = 0;
  4.     }
  5.     class SuperUser extends User {
  6.         public $role;
  7.        
  8.         public function __construct($role) {
  9.             if (!$role) {
  10.                     throw new Exception('Не заданно свойство');
  11.                 }
  12.                $this->role = $role;
  13.                SuperUser::$cont++;
  14.          
  15.         }
  16.        
  17.        
  18.         public function getInfo() {
  19.           return "Hello, my role is " . $this->role . "<br/>";
  20.         }
  21.     }
  22.    
  23.     //исключение
  24.     try {
  25.    
  26.         $me = new SuperUser('programmer');
  27.         echo $me->getInfo();
  28.    
  29.         $you = new SuperUser();
  30.     } catch (Exception $e) {
  31.         echo 'Выброшено исключение: ',  $e->getMessage(), "<br/>";
  32.     }
  33.        
  34.     //подсчет объектов
  35.     echo SuperUser::$cont;
  36. ?>
Advertisement
Add Comment
Please, Sign In to add comment