diff --git a/src/php/access/PropertyAccess.php b/src/php/access/PropertyAccess.php index 1550173..fe09704 100644 --- a/src/php/access/PropertyAccess.php +++ b/src/php/access/PropertyAccess.php @@ -9,18 +9,25 @@ class PropertyAccess extends AbstractAccess { function __construct(object &$object, $name, ?array $params=null) { parent::__construct($params); $this->object =& $object; - $this->class = new ReflectionClass($object); $this->name = $name; + $class = new ReflectionClass($object); + try { + $property = $class->getProperty($this->name); + $property->setAccessible(true); + } catch (ReflectionException $e) { + $property = null; + } + $this->property = $property; $this->allowNull = $params["allow_null"] ?? true; $this->allowFalse = $params["allow_false"] ?? false; } protected object $object; - protected ReflectionClass $class; - protected string $name; + protected ?ReflectionProperty $property; + protected bool $allowNull; protected bool $allowFalse; @@ -31,23 +38,16 @@ class PropertyAccess extends AbstractAccess { } function exists(): bool { - return $this->class->hasProperty($this->name) + return $this->property !== null || property_exists($this->object, $this->name); } - protected function _property(): ReflectionProperty { - $property = $this->class->getProperty($this->name); - $property->setAccessible(true); - return $property; - } - protected function _get($default=null) { - try { - return $this->_property()->getValue($this->object); - } catch (ReflectionException $e) { - } $name = $this->name; - if (property_exists($this->object, $name)) { + $property = $this->property; + if ($property !== null) { + return $property->getValue($this->object); + } elseif (property_exists($this->object, $name)) { return $this->object->$name; } else { return $default; @@ -73,10 +73,11 @@ class PropertyAccess extends AbstractAccess { } protected function _set($value): void { - try { - $this->_property()->setValue($this->object, $value); - } catch (ReflectionException $e) { - $name = $this->name; + $name = $this->name; + $property = $this->property; + if ($property !== null) { + $property->setValue($this->object, $value); + } else { $this->object->$name = $value; } }