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
Hello Sebastian,
fixed in cvs - and btw it took me 5 seconds to fix - i bet you could have
done it yourself :-)
best regards
marcus
Sunday, December 11, 2005, 1:25:26 PM, you wrote:
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
Best regards,
Marcus