[wp-trac] [WordPress Trac] #18488: set_transient crashes when value parameter is a SimpleXMLElement object

WordPress Trac wp-trac at lists.automattic.com
Sun Aug 26 19:53:08 UTC 2012


#18488: set_transient crashes when value parameter is a SimpleXMLElement object
--------------------------+-----------------------
 Reporter:  bobbingwide   |       Owner:
     Type:  enhancement   |      Status:  reopened
 Priority:  normal        |   Milestone:
Component:  Cache         |     Version:  3.2.1
 Severity:  normal        |  Resolution:
 Keywords:  dev-feedback  |
--------------------------+-----------------------
Changes (by mikeschinkel):

 * cc: mikeschinkel@… (added)
 * keywords:   => dev-feedback
 * status:  closed => reopened
 * resolution:  invalid =>
 * type:  defect (bug) => enhancement


Comment:

 '''Proposed potential solution?'''  I recently had exactly this need,
 which I think is common need for caching return values of APIs whose data
 is slow moving.  The solution I found was to convert the SimpleAPI object
 an array of objects using a variation of
 [http://www.bookofzeus.com/articles/convert-simplexml-object-into-php-
 array/ this function]:

 {{{
 function SimpleXMLElement_to_array( $xml ) {
   $arr = array();
   foreach ($xml as $element) {
     $tag = $element->getName();
     $e = get_object_vars($element);
     if (!empty($e)) {
       $arr[$tag] = $element instanceof SimpleXMLElement ?
 $this->SimpleXMLElement_to_array($element) : $e;
     }
     else {
       $arr[$tag] = trim($element);
     }
   }
   return (object)$arr;
 }
 }}}

 Could we add a similar function to WordPress ''(maybe
 `wp_SimpleXMLElement_to_array()`)'' so that people can easily convert
 `SimpleXMLElement`s to usable formats and also have the `set_transient()`
 use this function to enable `SimpleXMLElement`s to be saved without
 requiring the developer to handle it explicitly?  The `SimpleXMLElement`
 could be reconstituted in `get_transient()` for parity between set and
 get.

 Alternately still include the function but throw an error indicating the
 user is trying to save a `SimpleXMLElement` and tell them they need to use
 `wp_SimpleXMLElement_to_array()` to transform it first?

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/18488#comment:10>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list