Advertisement
Guest User

Untitled

a guest
May 21st, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.55 KB | None | 0 0
  1. trait MagicDataSet
  2. {
  3.     public function getAttributeValue(string $attribute)
  4.     {
  5.         if (!$this->hasAttribute($attribute)) {
  6.             throw new MissingAttributeException('$attribute is missing');
  7.         }
  8.  
  9.         return get_object_vars($this)[$attribute];
  10.     }
  11.  
  12.     public function hasAttribute(string $attribute): bool
  13.     {
  14.         return array_key_exists($attribute, get_object_vars($this));
  15.     }
  16. }
  17.  
  18. class Post implements DataSetInterface
  19. {
  20.     use MagicDataSet;
  21.  
  22.     public string $title;
  23.     public string $description;
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement