Skip to content

Instantly share code, notes, and snippets.

@bangpound
Created July 24, 2015 18:48
Show Gist options
  • Save bangpound/00e780703d5ac6749a49 to your computer and use it in GitHub Desktop.
Save bangpound/00e780703d5ac6749a49 to your computer and use it in GitHub Desktop.
Tracing private property values at runtime.
<?php
function init_trace($object)
{
    $r = new ReflectionObject($object);

    // Create array of object's property names.
    $properties = array_map(function (ReflectionProperty $property) {
        return $property->getName();
    }, $r->getProperties());

    // Bind a closure to the scope of the object with private properties.
    $tracer = Closure::bind(function ($object) use ($properties) {

        // Return the value of each property.
        return array_map(function ($property) use ($object) {
            return $object->{$property};
        }, $properties);

    }, null, $object);

    return function () use ($tracer, $object, $properties) {
        return array_combine($properties, $tracer($object));
    };
}
<?php
$loop = React\EventLoop\Factory::create();
$tracer = init_trace($loop);
dump($tracer());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment