note 80260 added to function.get-object-vars
| From: | (php@marcyes.com) | Date: | Mon, 07 Jan 2008 07:16:49 +0000 |
| Subject: | note 80260 added to function.get-object-vars | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-134909@lists.php.net to get a copy of this message | ||
This method returns all the public, private, and protected vars of a given object. There are some
other solutions presented in the comments but they didn't seem to preserve arrays, etc..
Hope it can help someone...
<?php
function parseObject($obj, $values=true){
$obj_dump = print_r($obj, 1);
$ret_list = array();
$ret_map = array();
$ret_name = '';
$dump_lines = preg_split('/[\r\n]+/',$obj_dump);
$ARR_NAME = 'arr_name';
$ARR_LIST = 'arr_list';
$arr_index = -1;
// get the object type...
$matches = array();
preg_match('/^\s*(\S+)\s+\bObject\b/i',$obj_dump,$matches);
if(isset($matches[1])){ $ret_name = $matches[1]; }//if
foreach($dump_lines as &$line){
$matches = array();
//load up var and values...
if(preg_match('/^\s*\[\s*(\S+)\s*\]\s+=>\s+(.*)$/', $line, $matches)){
if(mb_stripos($matches[2],'array') !== false){
$arr_map = array();
$arr_map[$ARR_NAME] = $matches[1];
$arr_map[$ARR_LIST] = array();
$arr_list[++$arr_index] = $arr_map;
}else{
// save normal variables and arrays differently...
if($arr_index >= 0){
$arr_list[$arr_index][$ARR_LIST][$matches[1]] = $matches[2];
}else{
$ret_list[$matches[1]] = $matches[2];
}//if/else
}//if/else
}else{
// save the current array to the return list...
if(mb_stripos($line,')') !== false){
if($arr_index >= 0){
$arr_map = array_pop($arr_list);
// if there is more than one array then this array belongs to the earlier array...
if($arr_index > 0){
$arr_list[($arr_index-1)][$ARR_LIST][$arr_map[$ARR_NAME]] = $arr_map[$ARR_LIST];
}else{
$ret_list[$arr_map[$ARR_NAME]] = $arr_map[$ARR_LIST];
}//if/else
$arr_index--;
}//if
}//if
}//if/else
}//foreach
$ret_map['name'] = $ret_name;
$ret_map['variables'] = $ret_list;
return $ret_map;
}//method
?>
----
Server IP: 69.147.83.197
Probable Submitter: 208.110.159.95
----
Manual Page -- http://www.php.net/manual/en/function.get-object-vars.php
Edit -- https://master.php.net/note/edit/80260
Del: integrated -- https://master.php.net/note/delete/80260/integrated
Del: useless -- https://master.php.net/note/delete/80260/useless
Del: bad code -- https://master.php.net/note/delete/80260/bad+code
Del: spam -- https://master.php.net/note/delete/80260/spam
Del: non-english -- https://master.php.net/note/delete/80260/non-english
Del: in docs -- https://master.php.net/note/delete/80260/in+docs
Del: other reasons-- https://master.php.net/note/delete/80260
Reject -- https://master.php.net/note/reject/80260
Search -- https://master.php.net/manage/user-notes.php