| 1: | <?php |
| 2: | |
| 3: | namespace Teto\Object; |
| 4: | |
| 5: | use function property_exists; |
| 6: | |
| 7: | /** |
| 8: | * Private property behaves like read only. |
| 9: | * |
| 10: | * NOTICE: You may not be able to imagine the behavior of this trait in the inherited class. |
| 11: | * |
| 12: | * @see \Teto\Object\PrivateGetterTest |
| 13: | * |
| 14: | * @author USAMI Kenta <tadsan@zonu.me> |
| 15: | * @copyright 2016 Baguette HQ |
| 16: | * @license http://www.apache.org/licenses/LICENSE-2.0 |
| 17: | */ |
| 18: | trait PrivateStrictGetter |
| 19: | { |
| 20: | public function __get($name) |
| 21: | { |
| 22: | if (property_exists($this, $name)) { |
| 23: | return $this->$name; |
| 24: | } |
| 25: | |
| 26: | throw new \OutOfRangeException("Unexpected key:'$name'"); |
| 27: | } |
| 28: | |
| 29: | public function __isset($name) |
| 30: | { |
| 31: | return isset($this->$name); |
| 32: | } |
| 33: | } |
| 34: |