The code below (which is based on Derick's code [1]) used to work until
recently:
<?php
class ClassWithNonPublicProperties {
protected $protectedProperty = 'foo';
private $privateProperty = 'bar';
}
function getNonPublicProperty($object, $propertyName) {
try {
$class = new ReflectionClass($object);
if ($class->hasProperty($propertyName)) {
$property = $class->getProperty($propertyName);
if (!$property->isPublic()) {
if ($property->isProtected()) {
$propertyName = "\0*\0" . $propertyName;
} else {
$propertyName = sprintf(
"\0%s\0%s",
get_class($object),
$propertyName
);
}
$tmp = (array) $object;
return $tmp[$propertyName];
} else {
return $object->$propertyName;
}
} else {
throw new InvalidArgumentException;
}
}
catch (ReflectionException $e) {
throw new InvalidArgumentException;
}
}
$object = new ClassWithNonPublicProperties;
print getNonPublicProperty($object, 'protectedProperty') . "\n";
print getNonPublicProperty($object, 'privateProperty') . "\n";
?>
The current PHP_5_1 prints
Warning: ReflectionClass::getProperty():
bad type specifier while parsing parameters in test.php on line 12
Fatal error: Call to a member function isPublic() on a non-object
in test.php on line 14
Is this change in behaviour intentional or should I file a bug report
for this?
--
[1] http://derickrethans.nl/private_properties_exposed.php
--
Sebastian Bergmann http://www.sebastian-bergmann.de/
GnuPG Key: 0xB85B5D69 / 27A7 2B14 09E4 98CD 6277 0E5B 6867 C514 B85B 5D69
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php