« Back to Index

PHP Reflection Example

View original Gist on GitHub

ReflectionExample.php

# http://net.tutsplus.com/tutorials/php/reflection-in-php/

class Some_TestCase extends PHPUnit_Framework_TestCase
{
    public function reflectValueIntoObject($object, $name, $value)
    {
        $refObject = new ReflectionObject($object);
        $refProperty = $refObject->getProperty($name);
        $refProperty->setAccessible(true);
        $refProperty->setValue(
            $object,
            $value
        ); # I find it so odd that we can call `setValue` on a property but the method needs the property's object to be specified?
    }
}