php-src/ext/reflection/tests/ReflectionProperty_setValue_error.phpt
Marco Pivetta 6e16e1daa9 Make ReflectionProperty/Method always accessible
With this patch, it is no longer required to call
`ReflectionProperty#setAccessible()` or
`ReflectionMethod#setAccessible()` with `true`.

If a userland consumer already got to the point of accessing
object/class information via reflection, it makes little sense
for `ext/reflection` to disallow accessing `private`/`protected`
symbols by default.

After this patch, calling `ReflectionProperty#setAccessible(true)`
or `ReflectionMethod#setAccessible(true)` on newly instantiated
`ReflectionProperty` or `ReflectionMethod` respectively will have
no effect.

RFC: https://wiki.php.net/rfc/make-reflection-setaccessible-no-op

Closes GH-5412.
2021-07-08 10:55:38 +02:00

39 lines
892 B
PHP

--TEST--
Test ReflectionProperty::setValue() error cases.
--FILE--
<?php
class TestClass {
public $pub;
public $pub2 = 5;
static public $stat = "static property";
protected $prot = 4;
private $priv = "keepOut";
}
class AnotherClass {
}
$instance = new TestClass();
$instanceWithNoProperties = new AnotherClass();
$propInfo = new ReflectionProperty('TestClass', 'pub2');
echo "\nProtected property:\n";
$propInfo = new ReflectionProperty('TestClass', 'prot');
$propInfo->setValue($instance, "NewValue");
var_dump($propInfo->getValue($instance));
echo "\n\nInstance without property:\n";
$propInfo = new ReflectionProperty('TestClass', 'pub2');
var_dump($propInfo->setValue($instanceWithNoProperties, "NewValue"));
var_dump($instanceWithNoProperties->pub2);
?>
--EXPECT--
Protected property:
string(8) "NewValue"
Instance without property:
NULL
string(8) "NewValue"