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