Skip to content

Instantly share code, notes, and snippets.

@J7mbo
Created April 9, 2013 14:49
Show Gist options
  • Save J7mbo/5346302 to your computer and use it in GitHub Desktop.
Save J7mbo/5346302 to your computer and use it in GitHub Desktop.
Haxx your way into a Class's private / protected variables using Reflection and variable variables! Pretty neat!
<?php
class Parameters
{
protected $var1;
protected $var2;
protected $var3;
public function __construct($one, $two, $three)
{
$this->var1 = $one;
$this->var2 = $two;
$this->var3 = $three;
}
}
// Note these become protected
$parameters = new Parameters('php', 'is', 'awesome');
$reflectHaxx = new ReflectionObject($parameters);
$parsHaxx = $reflectHaxx->getProperties();
foreach ($parsHaxx as $p)
{
// Get each property one by one...
$property = $reflectHaxx->getProperty($p->name);
// Set it to accessible
$property->setAccessible('true');
// Variable variable name and value declaration
${$p->name} = $property->getValue($parameters);
}
// Now you have access to the class variables wherever you are!
echo $var1;
echo $var2;
echo $var3;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment