modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2025-03-19 17:03:12 +04:00
parent 39af99ffa4
commit c5bfa3940c

View File

@ -9,18 +9,25 @@ class PropertyAccess extends AbstractAccess {
function __construct(object &$object, $name, ?array $params=null) { function __construct(object &$object, $name, ?array $params=null) {
parent::__construct($params); parent::__construct($params);
$this->object =& $object; $this->object =& $object;
$this->class = new ReflectionClass($object);
$this->name = $name; $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->allowNull = $params["allow_null"] ?? true;
$this->allowFalse = $params["allow_false"] ?? false; $this->allowFalse = $params["allow_false"] ?? false;
} }
protected object $object; protected object $object;
protected ReflectionClass $class;
protected string $name; protected string $name;
protected ?ReflectionProperty $property;
protected bool $allowNull; protected bool $allowNull;
protected bool $allowFalse; protected bool $allowFalse;
@ -31,23 +38,16 @@ class PropertyAccess extends AbstractAccess {
} }
function exists(): bool { function exists(): bool {
return $this->class->hasProperty($this->name) return $this->property !== null
|| property_exists($this->object, $this->name); || 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) { protected function _get($default=null) {
try {
return $this->_property()->getValue($this->object);
} catch (ReflectionException $e) {
}
$name = $this->name; $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; return $this->object->$name;
} else { } else {
return $default; return $default;
@ -73,10 +73,11 @@ class PropertyAccess extends AbstractAccess {
} }
protected function _set($value): void { protected function _set($value): void {
try { $name = $this->name;
$this->_property()->setValue($this->object, $value); $property = $this->property;
} catch (ReflectionException $e) { if ($property !== null) {
$name = $this->name; $property->setValue($this->object, $value);
} else {
$this->object->$name = $value; $this->object->$name = $value;
} }
} }