Created
September 26, 2018 13:21
-
-
Save juan-leon/cd52f9c9e7394d0897764fb2197e624b to your computer and use it in GitHub Desktop.
Objects cannot access his private attributes anymore while handling error, in PHP 7.2, v2
This file contains 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
#!/usr/bin/php -n | |
<?php | |
ini_set("display_errors", 1); | |
error_reporting(E_ALL); | |
class Foo { | |
private $dummy1 = 1; | |
private $dummy2 = 2; | |
function __construct() { | |
// This will create a "Undefined property" error when using reflection | |
// on that object and iterating over properties. This error is to be | |
// expected, and I am triggering it only to trigger the bug in the error | |
// handling. | |
unset($this->dummy2); | |
} | |
} | |
class ErrorHandler { | |
private $private; | |
function setup() { | |
set_error_handler( | |
function ($errno, $errstr, $errfile, $errline) { | |
$this->handleError($errno, $errstr, $errfile, $errline); | |
return true; | |
} | |
); | |
} | |
private function handleError($errno, $errstr, $errfile, $errline) { | |
echo "Dealing with error $errstr, $errfile, $errline\n"; | |
// This attribute is no longer accessible in this object. Same for other | |
// objects and their private attributes once we reach in this state. | |
$this->private; | |
echo "Never executed in PHP7.2; there is a fatal error in the line above\n"; | |
} | |
} | |
$errorHandler = new ErrorHandler(); | |
$errorHandler->setup(); | |
$foo = new Foo(); | |
// 1st stack frame contains the Foo object as 1st argument | |
$reflect = new ReflectionObject($foo); | |
$properties = $reflect->getProperties(); | |
foreach ($properties as $property) { | |
$property->setAccessible(true); | |
$propertyKey = $property->getName(); | |
// This will trigger an error for "dummy2" property | |
$value = $property->getValue($foo); | |
echo "property $propertyKey value is $value\n"; | |
} | |
echo "This line is not reached in PHP7.2; but it is reached in PHP 7.0\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment