From: Sebastian Bergmann Date: Sat, 29 Nov 2008 15:58:54 +0000 (+0000) Subject: MFH: Fix #46718: ReflectionProperty::setValue() and ReflectionProperty::setAccessible(). X-Git-Tag: php-5.3.0alpha2~41 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2f49305bc8923a4c92999250ffeabbb59ddcb0ae;p=php MFH: Fix #46718: ReflectionProperty::setValue() and ReflectionProperty::setAccessible(). --- diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index ccb21b9e23..01b516c275 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -4365,7 +4365,7 @@ ZEND_METHOD(reflection_property, setValue) METHOD_NOTSTATIC(reflection_property_ptr); GET_REFLECTION_OBJECT_PTR(ref); - if (!(ref->prop.flags & ZEND_ACC_PUBLIC)) { + if (!(ref->prop.flags & ZEND_ACC_PUBLIC) && ref->ignore_visibility == 0) { _default_get_entry(getThis(), "name", sizeof("name"), &name TSRMLS_CC); zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, "Cannot access non-public member %s::%s", intern->ce->name, Z_STRVAL(name)); @@ -4409,10 +4409,13 @@ ZEND_METHOD(reflection_property, setValue) zend_hash_quick_update(prop_table, ref->prop.name, ref->prop.name_length+1, ref->prop.h, &value, sizeof(zval *), (void **) &foo); } } else { + char *class_name, *prop_name; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "oz", &object, &value) == FAILURE) { return; } - zend_update_property(Z_OBJCE_P(object), object, ref->prop.name, ref->prop.name_length, value TSRMLS_CC); + zend_unmangle_property_name(ref->prop.name, ref->prop.name_length, &class_name, &prop_name); + zend_update_property(Z_OBJCE_P(object), object, prop_name, strlen(prop_name), value TSRMLS_CC); } } /* }}} */ diff --git a/ext/reflection/tests/reflectionProperty_setAccessible.phpt b/ext/reflection/tests/reflectionProperty_setAccessible.phpt index c48a828e6b..927a8a8613 100644 --- a/ext/reflection/tests/reflectionProperty_setAccessible.phpt +++ b/ext/reflection/tests/reflectionProperty_setAccessible.phpt @@ -2,43 +2,81 @@ Test ReflectionProperty::setAccessible(). --FILE-- getValue($a)); } -$instance = new TestClass(); +catch (ReflectionException $e) { + var_dump($e->getMessage()); +} -echo "\nProtected property:\n"; -$propInfo = new ReflectionProperty('TestClass', 'prot'); try { - var_dump($propInfo->getValue($instance)); + var_dump($protectedStatic->getValue()); } -catch(Exception $exc) { - echo $exc->getMessage(), "\n"; + +catch (ReflectionException $e) { + var_dump($e->getMessage()); } -$propInfo->setAccessible(true); -var_dump($propInfo->getValue($instance)); +try { + var_dump($private->getValue($a)); +} + +catch (ReflectionException $e) { + var_dump($e->getMessage()); +} -$propInfo->setAccessible(false); try { - var_dump($propInfo->getValue($instance)); + var_dump($privateStatic->getValue()); } -catch(Exception $exc) { - echo $exc->getMessage(), "\n"; + +catch (ReflectionException $e) { + var_dump($e->getMessage()); } -?> ---EXPECTF-- -Protected property: -Cannot access non-public member TestClass::prot -int(4) -Cannot access non-public member TestClass::prot +$protected->setAccessible(TRUE); +$protectedStatic->setAccessible(TRUE); +$private->setAccessible(TRUE); +$privateStatic->setAccessible(TRUE); + +var_dump($protected->getValue($a)); +var_dump($protectedStatic->getValue()); +var_dump($private->getValue($a)); +var_dump($privateStatic->getValue()); + +$protected->setValue($a, 'e'); +$protectedStatic->setValue('f'); +$private->setValue($a, 'g'); +$privateStatic->setValue('h'); + +var_dump($protected->getValue($a)); +var_dump($protectedStatic->getValue()); +var_dump($private->getValue($a)); +var_dump($privateStatic->getValue()); +?> +--EXPECT-- +string(44) "Cannot access non-public member A::protected" +string(50) "Cannot access non-public member A::protectedStatic" +string(42) "Cannot access non-public member A::private" +string(48) "Cannot access non-public member A::privateStatic" +string(1) "a" +string(1) "b" +string(1) "c" +string(1) "d" +string(1) "e" +string(1) "f" +string(1) "g" +string(1) "h"