felipe          Wed Oct 29 13:34:09 2008 UTC

  Added files:                 (Branch: PHP_5_2)
    /php-src/ext/reflection/tests       bug46064.phpt bug46064_2.phpt 

  Modified files:              
    /php-src/ext/reflection     php_reflection.c 
  Log:
  - MFH: Fixed bug #46064 (Exception when creating ReflectionProperty object on 
dynamicly created property)
  
  
http://cvs.php.net/viewvc.cgi/php-src/ext/reflection/php_reflection.c?r1=1.164.2.33.2.53&r2=1.164.2.33.2.54&diff_format=u
Index: php-src/ext/reflection/php_reflection.c
diff -u php-src/ext/reflection/php_reflection.c:1.164.2.33.2.53 
php-src/ext/reflection/php_reflection.c:1.164.2.33.2.54
--- php-src/ext/reflection/php_reflection.c:1.164.2.33.2.53     Mon Aug 11 
22:08:58 2008
+++ php-src/ext/reflection/php_reflection.c     Wed Oct 29 13:34:08 2008
@@ -20,7 +20,7 @@
    +----------------------------------------------------------------------+
 */
 
-/* $Id: php_reflection.c,v 1.164.2.33.2.53 2008/08/11 22:08:58 cseiler Exp $ */
+/* $Id: php_reflection.c,v 1.164.2.33.2.54 2008/10/29 13:34:08 felipe Exp $ */
 
 #ifdef HAVE_CONFIG_H
 #include "config.h"
@@ -1341,6 +1341,9 @@
        if (modifiers & (ZEND_ACC_FINAL | ZEND_ACC_FINAL_CLASS)) {
                add_next_index_stringl(return_value, "final", 
sizeof("final")-1, 1);
        }
+       if (modifiers & ZEND_ACC_IMPLICIT_PUBLIC) {
+               add_next_index_stringl(return_value, "public", 
sizeof("public")-1, 1);
+       }
 
        /* These are mutually exclusive */
        switch (modifiers & ZEND_ACC_PPP_MASK) {
@@ -3136,9 +3139,25 @@
        }
 
        GET_REFLECTION_OBJECT_PTR(ce);
-       if (zend_hash_find(&ce->properties_info, name, name_len + 1, (void**) 
&property_info) == SUCCESS && (property_info->flags & ZEND_ACC_SHADOW) == 0) {
-               reflection_property_factory(ce, property_info, return_value 
TSRMLS_CC);
-               return;
+       if (zend_hash_find(&ce->properties_info, name, name_len + 1, (void**) 
&property_info) == SUCCESS) {
+               if ((property_info->flags & ZEND_ACC_SHADOW) == 0) {
+                       reflection_property_factory(ce, property_info, 
return_value TSRMLS_CC);
+                       return;
+               }
+       } else if (intern->obj) {
+               /* Check for dynamic properties */
+               if 
(zend_hash_exists(Z_OBJ_HT_P(intern->obj)->get_properties(intern->obj 
TSRMLS_CC), name, name_len+1)) {
+                       zend_property_info property_info_tmp;
+                       property_info_tmp.flags = ZEND_ACC_IMPLICIT_PUBLIC;
+                       property_info_tmp.name = name;
+                       property_info_tmp.name_length = name_len;
+                       property_info_tmp.h = zend_get_hash_value(name, 
name_len+1);
+                       property_info_tmp.doc_comment = NULL;
+                       property_info_tmp.ce = ce;
+
+                       reflection_property_factory(ce, &property_info_tmp, 
return_value TSRMLS_CC);
+                       return;
+               }
        }
        if ((tmp = strstr(name, "::")) != NULL) {
                classname_len = tmp - name;
@@ -3784,12 +3803,12 @@
 {
        zval *propname, *classname;
        char *name_str, *class_name, *prop_name;
-       int name_len;
+       int name_len, dynam_prop = 0;
        zval *object;
        reflection_object *intern;
        zend_class_entry **pce;
        zend_class_entry *ce;
-       zend_property_info *property_info;
+       zend_property_info *property_info = NULL;
        property_reference *reference;
 
        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zs", &classname, 
&name_str, &name_len) == FAILURE) {
@@ -3823,12 +3842,19 @@
        }
 
        if (zend_hash_find(&ce->properties_info, name_str, name_len + 1, (void 
**) &property_info) == FAILURE || (property_info->flags & ZEND_ACC_SHADOW)) {
-               zend_throw_exception_ex(reflection_exception_ptr, 0 TSRMLS_CC, 
-                       "Property %s::$%s does not exist", ce->name, name_str);
-               return;
+               /* Check for dynamic properties */
+               if (property_info == NULL && Z_TYPE_P(classname) == IS_OBJECT 
&& Z_OBJ_HT_P(classname)->get_properties) {
+                       if 
(zend_hash_exists(Z_OBJ_HT_P(classname)->get_properties(classname TSRMLS_CC), 
name_str, name_len+1)) {
+                               dynam_prop = 1;
+                       }
+               }
+               if (dynam_prop == 0) {
+                       zend_throw_exception_ex(reflection_exception_ptr, 0 
TSRMLS_CC, "Property %s::$%s does not exist", ce->name, name_str);
+                       return;
+               }
        }
        
-       if (!(property_info->flags & ZEND_ACC_PRIVATE)) {
+       if (dynam_prop == 0 && (property_info->flags & ZEND_ACC_PRIVATE) == 0) {
                /* we have to search the class hierarchy for this (implicit) 
public or protected property */
                zend_class_entry *tmp_ce = ce;
                zend_property_info *tmp_info;
@@ -3844,14 +3870,27 @@
        ZVAL_STRINGL(classname, ce->name, ce->name_length, 1);
        zend_hash_update(Z_OBJPROP_P(object), "class", sizeof("class"), (void 
**) &classname, sizeof(zval *), NULL);
        
-       zend_unmangle_property_name(property_info->name, 
property_info->name_length, &class_name, &prop_name);
        MAKE_STD_ZVAL(propname);
-       ZVAL_STRING(propname, prop_name, 1);
+       if (dynam_prop == 0) {
+               zend_unmangle_property_name(property_info->name, 
property_info->name_length, &class_name, &prop_name);
+               ZVAL_STRING(propname, prop_name, 1);
+       } else {
+               ZVAL_STRINGL(propname, name_str, name_len, 1);
+       }
        zend_hash_update(Z_OBJPROP_P(object), "name", sizeof("name"), (void **) 
&propname, sizeof(zval *), NULL);
 
        reference = (property_reference*) emalloc(sizeof(property_reference));
+       if (dynam_prop) {
+               reference->prop.flags = ZEND_ACC_IMPLICIT_PUBLIC;
+               reference->prop.name = Z_STRVAL_P(propname);
+               reference->prop.name_length = Z_STRLEN_P(propname);
+               reference->prop.h = zend_get_hash_value(name_str, name_len+1);
+               reference->prop.doc_comment = NULL;
+               reference->prop.ce = ce;
+       } else {
+               reference->prop = *property_info;
+       }
        reference->ce = ce;
-       reference->prop = *property_info;
        intern->ptr = reference;
        intern->free_ptr = 1;
        intern->ce = ce;
@@ -3897,7 +3936,7 @@
    Returns whether this property is public */
 ZEND_METHOD(reflection_property, isPublic)
 {
-       _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC);
+       _property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_PUBLIC 
| ZEND_ACC_IMPLICIT_PUBLIC);
 }
 /* }}} */
 
@@ -3959,7 +3998,7 @@
        METHOD_NOTSTATIC(reflection_property_ptr);
        GET_REFLECTION_OBJECT_PTR(ref);
 
-       if (!(ref->prop.flags & ZEND_ACC_PUBLIC)) {
+       if (!(ref->prop.flags & (ZEND_ACC_PUBLIC | ZEND_ACC_IMPLICIT_PUBLIC))) {
                _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));
@@ -4908,7 +4947,7 @@
        php_info_print_table_start();
        php_info_print_table_header(2, "Reflection", "enabled");
 
-       php_info_print_table_row(2, "Version", "$Id: php_reflection.c,v 
1.164.2.33.2.53 2008/08/11 22:08:58 cseiler Exp $");
+       php_info_print_table_row(2, "Version", "$Id: php_reflection.c,v 
1.164.2.33.2.54 2008/10/29 13:34:08 felipe Exp $");
 
        php_info_print_table_end();
 } /* }}} */

http://cvs.php.net/viewvc.cgi/php-src/ext/reflection/tests/bug46064.phpt?view=markup&rev=1.1
Index: php-src/ext/reflection/tests/bug46064.phpt
+++ php-src/ext/reflection/tests/bug46064.phpt
--TEST--
Bug #46064 (Exception when creating ReflectionProperty object on dynamicly 
created property)
--FILE--
<?php

class x {
        public $zzz = 2;
}

$o = new x;
$o->z = 1000;
$o->zzz = 3;

var_dump($h = new reflectionproperty($o, 'z'));
var_dump($h->isDefault());
var_dump($h->isPublic());
var_dump($h->isStatic());
var_dump($h->getName());
var_dump(Reflection::getModifierNames($h->getModifiers()));
var_dump($h->getValue($o));

print "---------------------------\n";
try {
        var_dump(new reflectionproperty($o, 'zz'));
} catch (Exception $e) {
        var_dump($e->getMessage());
}

var_dump(new reflectionproperty($o, 'zzz'));

class test {
        protected $a = 1;
}

class bar extends test {
        public function __construct() {
                $this->foobar = 2;
                $this->a = 200;
                
                $p = new reflectionproperty($this, 'a');
                $p->setAccessible(true);
                var_dump($p->getValue($this), $p->isDefault(), $p->isPublic());
                
                $p = new reflectionproperty($this, 'foobar');
                var_dump($p->getValue($this), $p->isDefault(), $p->isPublic());
        }
}

new bar;

?>
--EXPECTF--
object(ReflectionProperty)#2 (2) {
  [u"name"]=>
  unicode(1) "z"
  [u"class"]=>
  unicode(1) "x"
}
bool(false)
bool(true)
bool(false)
unicode(1) "z"
array(1) {
  [0]=>
  unicode(6) "public"
}
int(1000)
---------------------------
unicode(30) "Property x::$zz does not exist"
object(ReflectionProperty)#3 (2) {
  [u"name"]=>
  unicode(3) "zzz"
  [u"class"]=>
  unicode(1) "x"
}
int(200)
bool(true)
bool(false)
int(2)
bool(false)
bool(true)

http://cvs.php.net/viewvc.cgi/php-src/ext/reflection/tests/bug46064_2.phpt?view=markup&rev=1.1
Index: php-src/ext/reflection/tests/bug46064_2.phpt
+++ php-src/ext/reflection/tests/bug46064_2.phpt
--TEST--
Bug #46064.2 (Exception when creating ReflectionProperty object on dynamicly 
created property)
--FILE--
<?php

class foo { 
}

$x = new foo;
$x->test = 2000;


$p = new ReflectionObject($x);
var_dump($p->getProperty('test'));


class bar {
        public function __construct() {
                $this->a = 1;
        }
}

class test extends bar {
        private $b = 2;

        public function __construct() {
                parent::__construct();
                
                $p = new reflectionobject($this);
                var_dump($h = $p->getProperty('a'));
                var_dump($h->isDefault(), $h->isProtected(), $h->isPrivate(), 
$h->isPublic(), $h->isStatic());
                var_dump($p->getProperties());
        }
}

new test;

?>
--EXPECT--
object(ReflectionProperty)#3 (2) {
  [u"name"]=>
  unicode(4) "test"
  [u"class"]=>
  unicode(3) "foo"
}
object(ReflectionProperty)#5 (2) {
  [u"name"]=>
  unicode(1) "a"
  [u"class"]=>
  unicode(4) "test"
}
bool(false)
bool(false)
bool(false)
bool(true)
bool(false)
array(2) {
  [0]=>
  &object(ReflectionProperty)#6 (2) {
    [u"name"]=>
    unicode(1) "b"
    [u"class"]=>
    unicode(4) "test"
  }
  [1]=>
  &object(ReflectionProperty)#7 (2) {
    [u"name"]=>
    unicode(1) "a"
    [u"class"]=>
    unicode(4) "test"
  }
}

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to