Skip to content

Instantly share code, notes, and snippets.

@dwickstrom
Created March 30, 2015 11:01
Show Gist options
  • Save dwickstrom/031cde590a4441904f0e to your computer and use it in GitHub Desktop.
Save dwickstrom/031cde590a4441904f0e to your computer and use it in GitHub Desktop.
Assert state, hidden or else
/**
* @author David Wickström <davidwickstrom@gmail.com>
*/
trait AssertState
{
/**
* Access any value off any object given a property with any visibility mode
*
* @param $field
* @param $object
* @return mixed
*/
public function getField($field, $object)
{
if (!is_string($field)) {
throw new InvalidArgumentException('First argument must have string type.');
}
if (!is_object($object)) {
throw new InvalidArgumentException('Second argument must have object type.');
}
$clonedObject = clone $object;
$reflector = new ReflectionProperty(get_class($clonedObject), $field);
$reflector->setAccessible(true);
return $reflector->getValue($clonedObject);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment