Skip to content

Commit b785c6c

Browse files
committed
No longer invoke setAccessible() when on PHP 8.1+
See xp-framework/test#24
1 parent 9cb5fa8 commit b785c6c

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

src/main/php/lang/reflection/Method.class.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ public function closure(?object $instance= null) {
5151
public function invoke(?object $instance, $args= []) {
5252
try {
5353
$pass= PHP_VERSION_ID < 80000 && $args ? self::pass($this->reflect, $args) : $args;
54-
$this->reflect->setAccessible(true);
54+
55+
// TODO: Remove superfluous call to setAccessible() if on PHP8.1+
56+
// see https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
57+
PHP_VERSION_ID < 80100 && $this->reflect->setAccessible(true);
5558
return $this->reflect->invokeArgs($instance, $pass);
5659
} catch (ReflectionException|ArgumentCountError|TypeError $e) {
5760
throw new CannotInvoke($this, $e);

src/main/php/lang/reflection/Property.class.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function get(?object $instance) {
7777

7878
// TODO: Remove superfluous call to setAccessible() if on PHP8.1+
7979
// see https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
80-
$this->reflect->setAccessible(true);
80+
PHP_VERSION_ID < 80100 && $this->reflect->setAccessible(true);
8181
return $this->reflect->getValue($instance);
8282
} catch (ReflectionException $e) {
8383
throw new CannotAccess($this, $e);
@@ -100,7 +100,7 @@ public function set(?object $instance, $value) {
100100

101101
// TODO: Remove superfluous call to setAccessible() if on PHP8.1+
102102
// see https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
103-
$this->reflect->setAccessible(true);
103+
PHP_VERSION_ID < 80100 && $this->reflect->setAccessible(true);
104104
$this->reflect->setValue($instance, $value);
105105
return $value;
106106
} catch (ReflectionException $e) {

src/main/php/lang/reflection/Type.class.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,11 @@ public function initializer($function) {
170170
} else if ($this->reflect->hasMethod($function)) {
171171
$reflect= $this->reflect->getMethod($function);
172172
return new Initializer($this->reflect, $reflect, function($instance, $args) use($reflect) {
173-
$reflect->setAccessible(true);
173+
174+
// TODO: Remove superfluous call to setAccessible() if on PHP8.1+
175+
// see https://wiki.php.net/rfc/make-reflection-setaccessible-no-op
176+
PHP_VERSION_ID < 80100 && $reflect->setAccessible(true);
177+
174178
return $reflect->invokeArgs($instance, $args);
175179
});
176180
}

0 commit comments

Comments
 (0)