PHP Doku:: Set property accessibility - reflectionproperty.setaccessible.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzVariablen- und typbezogene ErweiterungenReflectionThe ReflectionProperty classReflectionProperty::setAccessible

Ein Service von Reinhard Neidl - Webprogrammierung.

The ReflectionProperty class

<<ReflectionProperty::isStatic

ReflectionProperty::setValue>>

ReflectionProperty::setAccessible

(PHP 5 >= 5.3.0)

ReflectionProperty::setAccessibleSet property accessibility

Beschreibung

public void ReflectionProperty::setAccessible ( bool $accessible )

Sets a property to be accessible. For example, it may allow protected and private properties to be accessed.

Parameter-Liste

accessible

TRUE to allow accessibility, or FALSE.

Rückgabewerte

Es wird kein Wert zurückgegeben.

Siehe auch


2 BenutzerBeiträge:
- Beiträge aktualisieren...
Yzmir Ramirez
16.10.2010 2:31
Have you tried:

<?php

echo "PHP Version: ".phpversion()."\n";

class
Foo
{
    private  
$bar  = "private";
    protected
$bar2 = "protected";
    public   
$bar3 = "public";
}

$obj = new Foo;

$arr = (array)$obj;

print_r($arr);
?>

Output:

PHP Version: 5.2.12
Array
(
    [Foobar] => private
    [*bar2] => protected
    [bar3] => public
)

PHP Version: 5.1.6
Array
(
    [Foobar] => private
    [*bar2] => protected
    [bar3] => public
)
Rob McVey
11.06.2010 19:11
If you are using < PHP 5.3 and need to get the private attributes and values, you can use this method:

This is what you are doing:

<?php
$obj_with_privates
= new MyObject();
$class     = get_class($obj_with_privates);
$vars     = get_object_vars($obj_with_privates);
//will not show private attributes
print_r($vars);

$reflection = new ReflectionClass( $class );
$attributes = $reflection->getProperties();
//still no private access!
print_r($attributes);
?>

This is what you should do:

<?php
$obj_with_privates
= new MyObject();

$class         = get_class( $obj_with_privates );
$reflection = new ReflectionClass( $class );
$abstract    = $reflection->getMethods( ReflectionMethod::IS_ABSTRACT );
$priv_attr  = $reflection->getProperties( ReflectionProperty::IS_PRIVATE );
$privates   = array();
$parent     = get_parent_class( $class );
$child         = $class;
$constructor = $reflection->getConstructor();

//If the class has abstract methods you need to implement them
$abstr_methods = "";
if(
sizeof($abstr_methods))
{
    foreach(
$abstract as $method)
    {
       
$mname = $method->name;
       
$abstr_methods .= "public function $mname(){return false;}";
    }
}

//Convert private attributes to public attributes
if(sizeof($priv_attr))
{
   
$parseable = unserialize(str_replace("\0$class\0", "\0*\0", serialize($obj)));
    foreach(
$priv_attr as $attribute)
    {
       
$aname = $attribute->name;
       
$privates[$aname] = $parseable->$aname;
    }
}
           

$temp_child_class = "temp" . str_replace("_", "", "$class");

//You can gain access to protected attributes by extending the target class
$class_def = "
class
$temp_child_class extends $class{
   
$constructor
    public function reflect_getmyvars(){
        return get_object_vars(\$this);
    }
   
$abstr_methods
}
"
;

//place class definition in memory
eval($class_def);

//generate object from dynamic class
$tcobj =@ new $temp_child_class;   
//call the method we added to the object (to access protected vars)
$vars = $tcobj->reflect_getmyvars();

$attribs = array_merge($vars, $privates);

//will now show private attributes
print_r($attribs);
?>



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)