Skip to content

Instantly share code, notes, and snippets.

@leihog
Created November 15, 2015 12:07
Show Gist options
  • Save leihog/d17111f7b6e5128aad8c to your computer and use it in GitHub Desktop.
Save leihog/d17111f7b6e5128aad8c to your computer and use it in GitHub Desktop.
Example of how to use reflection to extract values from and restore a MangoPaySDK OAuthToken
use \MangoPay\Libraries\OAuthToken;
function extractValues(OAuthToken $token) {
$ref = new \ReflectionClass(OAuthToken::CLASS);
$values = [];
$props = $ref->getProperties();
foreach($props as $prop) {
if (!$prop->isPublic()) {
$prop->setAccessible(true);
}
$values[$prop->getName()] = $prop->getValue($token);
}
return $values;
}
function restoreToken(array $values) {
$ref = new \ReflectionClass(OAuthToken::CLASS);
$obj = $ref->newInstanceWithoutConstructor();
$props = $ref->getProperties();
foreach($props as $prop) {
$name = $prop->getName();
if (array_key_exists($name, $values)) {
if (!$prop->isPublic()) {
$prop->setAccessible(true);
}
$prop->setValue($obj, $values[$name]);
}
}
return $obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment