]> granicus.if.org Git - php/commitdiff
- Add the ReflectionProperty::setAccesible() method that allows non-public
authorDerick Rethans <derick@php.net>
Fri, 15 Feb 2008 12:37:38 +0000 (12:37 +0000)
committerDerick Rethans <derick@php.net>
Fri, 15 Feb 2008 12:37:38 +0000 (12:37 +0000)
  property's values to be read through ::getValue().

ext/reflection/php_reflection.c
ext/reflection/tests/reflectionProperty_setAccesible.phpt [new file with mode: 0644]

index 58a9ed843d0f3be11001d2368b1e8db1113492fd..3a13ba359ad3ade8c3fe7fd4f209bedafe52c0ea 100644 (file)
@@ -175,6 +175,7 @@ static void string_free(string *str)
 typedef struct _property_reference {
        zend_class_entry *ce;
        zend_property_info prop;
+       unsigned int ignore_visibility:1;
 } property_reference;
 
 /* Struct for parameters */
@@ -1196,6 +1197,7 @@ static void reflection_property_factory(zend_class_entry *ce, zend_property_info
        reference = (property_reference*) emalloc(sizeof(property_reference));
        reference->ce = ce;
        reference->prop = *prop;
+       reference->ignore_visibility = 0;
        intern->ptr = reference;
        intern->free_ptr = 1;
        intern->ce = ce;
@@ -3939,6 +3941,7 @@ ZEND_METHOD(reflection_property, __construct)
        reference = (property_reference*) emalloc(sizeof(property_reference));
        reference->ce = ce;
        reference->prop = *property_info;
+       reference->ignore_visibility = 0;
        intern->ptr = reference;
        intern->free_ptr = 1;
        intern->ce = ce;
@@ -4078,7 +4081,7 @@ ZEND_METHOD(reflection_property, getValue)
        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 %v::%v", intern->ce->name, Z_UNIVAL(name));
@@ -4226,6 +4229,24 @@ ZEND_METHOD(reflection_property, getDocComment)
 }
 /* }}} */
 
+/* {{{ proto public int ReflectionProperty::setAccesible() U
+   Sets whether non-public properties can be requested */
+ZEND_METHOD(reflection_property, setAccesible)
+{
+       reflection_object *intern;
+       property_reference *ref;
+       zend_bool visible;
+
+       METHOD_NOTSTATIC_NUMPARAMS(reflection_property_ptr, 1);
+       GET_REFLECTION_OBJECT_PTR(ref);
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "b", &visible) == FAILURE) {
+               return;
+       }
+       ref->ignore_visibility = visible;
+}
+/* }}} */
+
 /* {{{ proto public static mixed ReflectionExtension::export(string name [, bool return]) throws ReflectionException U
    Exports a reflection object. Returns the output if TRUE is specified for return, printing it otherwise. */
 ZEND_METHOD(reflection_extension, export)
@@ -4837,6 +4858,11 @@ ZEND_BEGIN_ARG_INFO(arginfo_reflection_property_setValue, 0)
        ZEND_ARG_INFO(0, value)
 ZEND_END_ARG_INFO()
 
+static
+ZEND_BEGIN_ARG_INFO(arginfo_reflection_property_setAccesible, 0)
+       ZEND_ARG_INFO(0, value)
+ZEND_END_ARG_INFO()
+
 static const zend_function_entry reflection_property_functions[] = {
        ZEND_ME(reflection, __clone, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
        ZEND_ME(reflection_property, export, arginfo_reflection_property_export, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
@@ -4854,6 +4880,7 @@ static const zend_function_entry reflection_property_functions[] = {
        ZEND_ME(reflection_property, getDefaultValue, NULL, 0)
        ZEND_ME(reflection_property, getDeclaringClass, NULL, 0)
        ZEND_ME(reflection_property, getDocComment, NULL, 0)
+       ZEND_ME(reflection_property, setAccesible, arginfo_reflection_property_setAccesible, 0)
        {NULL, NULL, NULL}
 };
 
diff --git a/ext/reflection/tests/reflectionProperty_setAccesible.phpt b/ext/reflection/tests/reflectionProperty_setAccesible.phpt
new file mode 100644 (file)
index 0000000..f96dac7
--- /dev/null
@@ -0,0 +1,46 @@
+--TEST--
+Test ReflectionProperty::setAccesible().
+--SKIPIF--
+<?php extension_loaded('reflection') or die('skip'); ?>
+--FILE--
+<?php
+
+class TestClass {
+    public $pub;
+    public $pub2 = 5;
+    static public $stat = "static property";
+    protected $prot = 4;
+    private $priv = "keepOut";
+}
+
+class AnotherClass {
+}
+
+$instance = new TestClass();
+
+echo "\nProtected property:\n";
+$propInfo = new ReflectionProperty('TestClass', 'prot');
+try {
+    var_dump($propInfo->getValue($instance));
+}
+catch(Exception $exc) {
+    echo $exc->getMessage(), "\n";
+}
+
+$propInfo->setAccesible(true);
+var_dump($propInfo->getValue($instance));
+
+$propInfo->setAccesible(false);
+try {
+    var_dump($propInfo->getValue($instance));
+}
+catch(Exception $exc) {
+    echo $exc->getMessage(), "\n";
+}
+?>
+--EXPECTF--
+
+Protected property:
+Cannot access non-public member TestClass::prot
+int(4)
+Cannot access non-public member TestClass::prot