mirror of
https://github.com/php/php-src.git
synced 2026-04-03 06:02:23 +02:00
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.
116 lines
2.6 KiB
PHP
116 lines
2.6 KiB
PHP
--TEST--
|
|
Test ReflectionProperty::isInitialized()
|
|
--FILE--
|
|
<?php
|
|
|
|
class A {
|
|
public static ?string $ssv = null;
|
|
public static ?string $ss;
|
|
public static $s;
|
|
public ?int $iv = null;
|
|
public ?int $i;
|
|
public $n;
|
|
|
|
private int $p;
|
|
}
|
|
|
|
class B extends A { }
|
|
|
|
echo "Static properties:\n";
|
|
var_dump((new ReflectionProperty(A::class, 'ssv'))->isInitialized());
|
|
var_dump((new ReflectionProperty(A::class, 'ss'))->isInitialized());
|
|
var_dump((new ReflectionProperty(A::class, 's'))->isInitialized());
|
|
|
|
echo "Declared properties:\n";
|
|
$a = new A;
|
|
var_dump((new ReflectionProperty($a, 'iv'))->isInitialized($a));
|
|
var_dump((new ReflectionProperty($a, 'i'))->isInitialized($a));
|
|
var_dump((new ReflectionProperty($a, 'n'))->isInitialized($a));
|
|
|
|
echo "Declared properties after unset:\n";
|
|
unset($a->iv);
|
|
unset($a->i);
|
|
unset($a->n);
|
|
var_dump((new ReflectionProperty($a, 'i'))->isInitialized($a));
|
|
var_dump((new ReflectionProperty($a, 'iv'))->isInitialized($a));
|
|
var_dump((new ReflectionProperty($a, 'n'))->isInitialized($a));
|
|
|
|
echo "Dynamic properties:\n";
|
|
$a->d = null;
|
|
$rp = new ReflectionProperty($a, 'd');
|
|
var_dump($rp->isInitialized($a));
|
|
unset($a->d);
|
|
var_dump($rp->isInitialized($a));
|
|
|
|
echo "Visibility handling:\n";
|
|
$rp = new ReflectionProperty('A', 'p');
|
|
var_dump($rp->isInitialized($a));
|
|
var_dump($rp->isInitialized($a));
|
|
|
|
echo "Object type:\n";
|
|
$rp = new ReflectionProperty('B', 'i');
|
|
var_dump($rp->isInitialized($a));
|
|
|
|
try {
|
|
var_dump($rp->isInitialized(new stdClass));
|
|
} catch (ReflectionException $e) {
|
|
echo $e->getMessage(), "\n";
|
|
}
|
|
|
|
try {
|
|
var_dump($rp->isInitialized());
|
|
} catch (TypeError $e) {
|
|
echo $e->getMessage(), "\n";
|
|
}
|
|
|
|
class WithMagic {
|
|
public $prop;
|
|
public int $intProp;
|
|
|
|
public function __isset($name) {
|
|
echo "__isset($name)\n";
|
|
return true;
|
|
}
|
|
|
|
public function __get($name) {
|
|
echo "__get($name)\n";
|
|
return "foobar";
|
|
}
|
|
}
|
|
|
|
echo "Class with __isset:\n";
|
|
$obj = new WithMagic;
|
|
unset($obj->prop);
|
|
$rp = new ReflectionProperty('WithMagic', 'prop');
|
|
var_dump($rp->isInitialized($obj));
|
|
$rp = new ReflectionProperty('WithMagic', 'intProp');
|
|
var_dump($rp->isInitialized($obj));
|
|
|
|
?>
|
|
--EXPECT--
|
|
Static properties:
|
|
bool(true)
|
|
bool(false)
|
|
bool(true)
|
|
Declared properties:
|
|
bool(true)
|
|
bool(false)
|
|
bool(true)
|
|
Declared properties after unset:
|
|
bool(false)
|
|
bool(false)
|
|
bool(false)
|
|
Dynamic properties:
|
|
bool(true)
|
|
bool(false)
|
|
Visibility handling:
|
|
bool(false)
|
|
bool(false)
|
|
Object type:
|
|
bool(false)
|
|
Given object is not an instance of the class this property was declared in
|
|
ReflectionProperty::isInitialized(): Argument #1 ($object) must be provided for instance properties
|
|
Class with __isset:
|
|
bool(false)
|
|
bool(false)
|