Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.98 KB | None | 0 0
  1. /**
  2.  * Class Competence
  3.  * Пример утиной типизации.
  4.  * Если нечто ходит как утка и крякает как утка, то это утка
  5.  */
  6. class Competence {
  7.     public $mood;
  8.     public $name;
  9.  
  10.     /**
  11.      * Competence constructor.
  12.      * @param $mood
  13.      * @param $name
  14.      */
  15.     public function __construct($mood, $name)
  16.     {
  17.         $this->mood = $mood;
  18.         $this->name = $name;
  19.     }
  20.  
  21.  
  22.     public function equal($obj) {
  23.         $cl = new ReflectionClass($obj);
  24.         if ($cl->hasProperty('mood') && $cl->hasProperty('name')) {
  25.             if ($obj->mood == $this->mood && $obj->name == $this->name)
  26.                 return true;
  27.         }
  28.         return false;
  29.     }
  30. }
  31.  
  32. class SomeClass
  33. {
  34.     public $some_thing;
  35. }
  36.  
  37. $competence = new Competence('good', 'Programming');
  38. $compy = new Competence('bad', 'Programming');
  39. $smth = new SomeClass();
  40.  
  41. echo $competence->equal($compy);
  42. echo $competence->equal($smth);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement