]> granicus.if.org Git - php/commitdiff
- Add ReflectionProperty::getDefaultValue()
authorMarcus Boerger <helly@php.net>
Tue, 21 Feb 2006 00:37:39 +0000 (00:37 +0000)
committerMarcus Boerger <helly@php.net>
Tue, 21 Feb 2006 00:37:39 +0000 (00:37 +0000)
ext/reflection/php_reflection.c
ext/reflection/tests/008.phpt [new file with mode: 0755]

index a5c5dfcf8276902b07bb056f5f590e50545ab1a0..d7f4d8e4de72af697c9b0a97e3a0eb27d8641954 100644 (file)
@@ -1106,15 +1106,17 @@ static void reflection_property_factory(zend_class_entry *ce, zend_property_info
        zval *classname;
        property_reference *reference;
        char *class_name, *prop_name;
+       zend_uchar utype = UG(unicode) ? IS_UNICODE : IS_STRING;
 
-       zend_u_unmangle_property_name(UG(unicode)?IS_UNICODE:IS_STRING, prop->name, &class_name, &prop_name);
-
+       zend_u_unmangle_property_name(utype, prop->name, &class_name, &prop_name);
+       
        if (!(prop->flags & ZEND_ACC_PRIVATE)) {
                /* we have to seach the class hierarchy for this (implicit) public or protected property */
                zend_class_entry *tmp_ce = ce;
                zend_property_info *tmp_info;
+               int prop_name_len = UG(unicode) ? u_strlen((UChar*)prop_name) : strlen(prop_name);
                
-               while (tmp_ce && zend_hash_find(&tmp_ce->properties_info, prop_name, strlen(prop_name) + 1, (void **) &tmp_info) != SUCCESS) {
+               while (tmp_ce && zend_u_hash_find(&tmp_ce->properties_info, utype, prop_name, prop_name_len + 1, (void **) &tmp_info) != SUCCESS) {
                        ce = tmp_ce;
                        prop = tmp_info;
                        tmp_ce = tmp_ce->parent;
@@ -3716,6 +3718,36 @@ ZEND_METHOD(reflection_property, isDefault)
 }
 /* }}} */
 
+/* {{{ proto public mixed ReflectionProperty::getDefaultValue()
+   Returns the default value of the property. */
+ZEND_METHOD(reflection_property, getDefaultValue)
+{
+       reflection_object *intern;
+       property_reference *ref;
+       HashTable *prop_defaults;
+       zval **zdef, *zv;
+       zend_uchar utype = UG(unicode) ? IS_UNICODE : IS_STRING;
+
+       METHOD_NOTSTATIC_NUMPARAMS(reflection_property_ptr, 0);
+       GET_REFLECTION_OBJECT_PTR(ref);
+
+       if (ref->prop->flags & ZEND_ACC_STATIC) {
+               prop_defaults = &ref->ce->default_static_members;
+       } else {
+               prop_defaults = &ref->ce->default_properties;
+       }
+
+       if (zend_u_hash_quick_find(prop_defaults, utype, ref->prop->name, ref->prop->name_length+1, ref->prop->h, (void**)&zdef) == SUCCESS) {
+               ALLOC_ZVAL(zv);
+               *zv = **zdef;
+               zval_copy_ctor(zv);
+               INIT_PZVAL(zv);
+               zval_update_constant(&zv, (void*)1 TSRMLS_CC);
+               RETURN_ZVAL(zv, 1, 1);
+       }
+}
+/* }}} */
+
 /* {{{ proto public int ReflectionProperty::getModifiers()
    Returns a bitfield of the access modifiers for this property */
 ZEND_METHOD(reflection_property, getModifiers)
@@ -4287,6 +4319,7 @@ static zend_function_entry reflection_property_functions[] = {
        ZEND_ME(reflection_property, isStatic, NULL, 0)
        ZEND_ME(reflection_property, isDefault, NULL, 0)
        ZEND_ME(reflection_property, getModifiers, NULL, 0)
+       ZEND_ME(reflection_property, getDefaultValue, NULL, 0)
        ZEND_ME(reflection_property, getDeclaringClass, NULL, 0)
        ZEND_ME(reflection_property, getDocComment, NULL, 0)
        {NULL, NULL, NULL}
diff --git a/ext/reflection/tests/008.phpt b/ext/reflection/tests/008.phpt
new file mode 100755 (executable)
index 0000000..4869a96
--- /dev/null
@@ -0,0 +1,71 @@
+--TEST--
+ReflectionProperty::getDefaultValue()
+--FILE--
+<?php
+
+
+class root
+{
+       public    $rPub = "rPub";
+       public    $xPub = "xPub";
+       protected $rPro = "rPro";
+       protected $xPro = "xPub";
+       private   $rPri = "rPri";
+       private   $xPri = "xPri";
+       
+       static public $stat = "rStat";
+}
+
+class derived extends root
+{
+       public    $dPub = "dPub";
+       public    $xPub = "nPub";
+       protected $dPro = "dPro";
+       protected $xPro = "nPub";
+       private   $dPri = "dPri";
+       private   $xPri = "nPri";       
+}
+
+function show_prop($p)
+{
+       echo "{$p->class}::{$p->name} = " . $p->getDefaultValue() . "\n";
+}
+
+function show_class($c)
+{
+       echo "===$c===\n";
+
+       $r = new ReflectionClass($c);
+
+       foreach($r->getProperties() as $p)
+       {
+               show_prop($p);
+       }
+}
+
+show_class("root");
+show_class("derived");
+
+?>
+===DONE===
+<?php exit(0); ?>
+--EXPECTF--
+===root===
+root::rPub = rPub
+root::xPub = xPub
+root::rPro = rPro
+root::xPro = xPub
+root::rPri = rPri
+root::xPri = xPri
+root::stat = rStat
+===derived===
+derived::dPub = dPub
+derived::xPub = nPub
+derived::dPro = dPro
+derived::xPro = nPub
+derived::dPri = dPri
+derived::xPri = nPri
+derived::rPub = rPub
+derived::rPro = rPro
+derived::stat = rStat
+===DONE===