-
-
Save wjaspers/9353164 to your computer and use it in GitHub Desktop.
PHP stdClass reflection bug
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 | |
// @link https://bugs.php.net/bug.php?id=66821 | |
class Helper | |
{ | |
protected $reflector; | |
protected $object; | |
public function __construct($object) | |
{ | |
$this->object = $object; | |
$this->reflector = new ReflectionClass($object); | |
} | |
public function __get($property) | |
{ | |
$property = $this->reflector->getProperty($property); | |
$property->setAccessible(true); | |
return $property->getValue($this->object); | |
} | |
} | |
class TestObj | |
{ | |
public $publicNullVar = null; | |
public $publicIntVar = 1; | |
protected $protectedNullVar = null; | |
protected $protectedIntVar = 1; | |
} | |
$testObj = new TestObj; | |
$stdObj = new stdClass; | |
$stdObj->publicNullVar = null; | |
$stdObj->publicIntVar = 1; | |
$testHelper = new Helper($testObj); | |
var_dump($testHelper->publicNullVar); | |
$stdHelper = new Helper($stdObj); | |
var_dump($stdHelper->publicNullVar); |
Expected Output:
NULL
NULL
Welp, I'm a dolt.
PHP has a ReflectionClass
AND a ReflectionObject
.
The latter works for inspecting objects correctly, and doesn't act strangely with stdClass objects.
Thanks to the PHP community for resolving this issue so quickly!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: