1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39:
<?php
namespace Teto\Object;
trait PropertyLikeMethod
{
public function __get($name)
{
if (!isset(self::$property_like_methods)) {
throw new \LogicException(static::class.'::$property_like_methods is not set.');
}
if (isset(self::$property_like_methods[$name])) {
$method = self::$property_like_methods[$name];
} elseif (in_array($name, self::$property_like_methods)) {
$method = $name;
} elseif (property_exists($this, $name)) {
return $this->$name;
} else {
throw new \OutOfRangeException("Unexpected key:'$name'");
}
return call_user_func([$this, $method]);
}
}