note 113971 added to reserved.classes

From: Date: Tue, 24 Dec 2013 01:15:13 +0000
Subject: note 113971 added to reserved.classes
Groups: php.notes 
Request: Send a blank email to php-notes+get-198013@lists.php.net to get a copy of this message
If you call var_export() on an instance of stdClass, it attempts to export it using ::__set_state(), which, for some reason, is not implemented in stdClass. However, casting an associative array to an object usually produces the same effect (at least, it does in my case). So I wrote an improved_var_export() function to convert instances of stdClass to (object) array () calls. If you choose to export objects of any other class, I'd advise you to implement ::__set_state(). <?php /** * An implementation of var_export() that is compatible with instances * of stdClass. * @param mixed $variable The variable you want to export * @param bool $return If used and set to true, improved_var_export() * will return the variable representation instead of outputting it. * @return mixed|null Returns the variable representation when the * return parameter is used and evaluates to TRUE. Otherwise, this * function will return NULL. */ function improved_var_export ($variable, $return = false) { if ($variable instanceof stdClass) { $result = '(object) '.improved_var_export(get_object_vars($variable), true); } else if (is_array($variable)) { $array = array (); foreach ($variable as $key => $value) { $array[] = var_export($key, true).' => '.improved_var_export($value, true); } $result = 'array ('.implode(', ', $array).')'; } else { $result = var_export($variable, true); } if (!$return) { print $result; return null; } else { return $result; } } // Example usage: $obj = new stdClass; $obj->test = 'abc'; $obj->other = 6.2; $obj->arr = array (1, 2, 3); improved_var_export((object) array ( 'prop1' => true, 'prop2' => $obj, 'assocArray' => array ( 'apple' => 'good', 'orange' => 'great' ) )); /* Output: (object) array ('prop1' => true, 'prop2' => (object) array ('test' => 'abc', 'other' => 6.2, 'arr' => array (0 => 1, 1 => 2, 2 => 3)), 'assocArray' => array ('apple' => 'good', 'orange' => 'great')) */ ?> Note: This function spits out a single line of code, which is useful to save in a cache file to include/eval. It isn't formatted for readability. If you want to print a readable version for debugging purposes, then I would suggest print_r() or var_dump(). ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/reserved.classes.php Edit -- https://master.php.net/note/edit/113971 Del: integrated -- https://master.php.net/note/delete/113971/integrated Del: useless -- https://master.php.net/note/delete/113971/useless Del: bad code -- https://master.php.net/note/delete/113971/bad+code Del: spam -- https://master.php.net/note/delete/113971/spam Del: non-english -- https://master.php.net/note/delete/113971/non-english Del: in docs -- https://master.php.net/note/delete/113971/in+docs Del: other reasons-- https://master.php.net/note/delete/113971 Reject -- https://master.php.net/note/reject/113971 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198013) next »