Skip to content

Instantly share code, notes, and snippets.

@absent1706
Created November 14, 2016 12:17
Show Gist options
  • Save absent1706/2ff06f128ad575b4b3d7007767206234 to your computer and use it in GitHub Desktop.
Save absent1706/2ff06f128ad575b4b3d7007767206234 to your computer and use it in GitHub Desktop.
PHP: Reflect provate properties
<?php
/* Returns all properties og object including private/protected
* in [prop => value] array
*/
function props($obj) {
$reflect = new \ReflectionClass($obj);
$props = $reflect->getProperties();
$result = [];
foreach ($props as $prop) {
$prop->setAccessible(true);
$name = $prop->name;
$result[$prop->name] = $prop->getValue($obj);
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment