BaguettePHP /
objectsystem
| 1 | <?php |
||
| 2 | namespace Teto\Object; |
||
| 3 | |||
| 4 | /** |
||
| 5 | * Make Property like method |
||
| 6 | * |
||
| 7 | * @author USAMI Kenta <[email protected]> |
||
| 8 | * @copyright 2016 Baguette HQ |
||
| 9 | * @license http://www.apache.org/licenses/LICENSE-2.0 |
||
| 10 | */ |
||
| 11 | trait PropertyLikeMethod |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * Call method as property (magic method) |
||
| 15 | * |
||
| 16 | * @param string $name |
||
| 17 | * @return mixed |
||
| 18 | * @see http://php.net/manual/language.oop5.magic.php |
||
| 19 | * @suppress PhanUndeclaredStaticProperty |
||
| 20 | */ |
||
| 21 | 2 | public function __get($name) |
|
| 22 | { |
||
| 23 | 2 | if (!isset(self::$property_like_methods)) { |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 24 | throw new \LogicException(static::class.'::$property_like_methods is not set.'); |
||
| 25 | } |
||
| 26 | |||
| 27 | 2 | if (isset(self::$property_like_methods[$name])) { |
|
| 28 | 1 | $method = self::$property_like_methods[$name]; |
|
| 29 | 2 | } elseif (in_array($name, self::$property_like_methods)) { |
|
| 30 | 1 | $method = $name; |
|
| 31 | 1 | } elseif (property_exists($this, $name)) { |
|
| 32 | return $this->$name; |
||
| 33 | } else { |
||
| 34 | 1 | throw new \OutOfRangeException("Unexpected key:'$name'"); |
|
| 35 | } |
||
| 36 | |||
| 37 | 1 | return call_user_func([$this, $method]); |
|
| 38 | } |
||
| 39 | } |
||
| 40 |