Created
April 9, 2013 14:49
-
-
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!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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