note 98383 added to reflectionproperty.setaccessible
| From: | RobMcVey at osu1 dot php dot net | Date: | Fri, 11 Jun 2010 17:11:58 +0000 |
| Subject: | note 98383 added to reflectionproperty.setaccessible | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-170067@lists.php.net to get a copy of this message | ||
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:
$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:
$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);
----
Server IP: 69.147.83.197
Probable Submitter: 68.188.14.110
----
Manual Page -- http://www.php.net/manual/en/reflectionproperty.setaccessible.php
Edit -- https://master.php.net/note/edit/98383
Del: integrated -- https://master.php.net/note/delete/98383/integrated
Del: useless -- https://master.php.net/note/delete/98383/useless
Del: bad code -- https://master.php.net/note/delete/98383/bad+code
Del: spam -- https://master.php.net/note/delete/98383/spam
Del: non-english -- https://master.php.net/note/delete/98383/non-english
Del: in docs -- https://master.php.net/note/delete/98383/in+docs
Del: other reasons-- https://master.php.net/note/delete/98383
Reject -- https://master.php.net/note/reject/98383
Search -- https://master.php.net/manage/user-notes.php