Lazy Loading Object Dumper for CPHP

This is a helpful method I find myself using frequently when debugging Connect for PHP code. It will recursively load a CPHP object's properties and return them in a nested array for easy logging.

class Util { /** * Returns an array of all object properties (excluding '_metadata' property). * Useful for inspecting objects returned from ROQL queries and Connect * for PHP methods. This WILL trigger the lazy loading of lookup objects and * traverse them when $recursive is true. * @throws RightNow\Connect\v1\ConnectAPIErrorBase * @param mixed $object Any object * @param bool $recursive Optional: recursively get properties * from nested objects * @return mixed Array of object properties, or single property */ public static function getObjectProperties($object, $recursive = false) { // If property is object, recursively get sub properties if (is_object($object)) { $outputArray = array(); // Use reflection to get public and dynamic properties $reflect = new \ReflectionObject($object); $props = $reflect->getProperties(\ReflectionProperty::IS_PUBLIC); // Loop through property list foreach ($props as $prop) { $propName = $prop->getName(); // Exclude metadata from properties list if ($propName != "_metadata") { if ($recursive) { $outputArray[$propName] = self::getObjectProperties($object->$propName, $recursive); } else { $outputArray[$propName] = $object->$propName; } } } return $outputArray; } // If property is array, recursively get properties of elements elseif (is_array($object)) { $subArray = array(); foreach ($object as $idx => $propElem) { $subArray[$idx] = self::getObjectProperties($propElem, $recursive); } return $subArray; } // Non-object or non-array property else { return $object; } } }
Zircon - This is a contributing Drupal Theme
Design by WeebPal.