PropertyAccessTrait.php 979 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace fphammerle\helpers;
  3. trait PropertyAccessTrait
  4. {
  5. /**
  6. * @param string $name
  7. * @return mixed
  8. */
  9. public function __get($name)
  10. {
  11. $getter_name = 'get' . $name;
  12. if(!method_exists($this, $getter_name)) {
  13. throw new \Exception('unknown property ' . $name);
  14. } else {
  15. return $this->$getter_name();
  16. }
  17. }
  18. /**
  19. * @param string $name
  20. * @param mixed $value
  21. */
  22. public function __set($name, $value)
  23. {
  24. $setter_name = 'set' . $name;
  25. if(!method_exists($this, $setter_name)) {
  26. throw new \Exception('unknown property ' . $name);
  27. } else {
  28. $this->$setter_name($value);
  29. }
  30. }
  31. /**
  32. * @param string $name
  33. * @return boolean
  34. */
  35. public function __isset($name)
  36. {
  37. $getter_name = 'get' . $name;
  38. return method_exists($this, $getter_name) && ($this->$getter_name() !== null);
  39. }
  40. }