Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Objects cannot access his private attributes anymore while handling error, in PHP 7.2
#!/usr/bin/php -n
ini_set("display_errors", 1);
class Foo {
private $dummy1 = 1;
private $dummy2 = 2;
function unsetProperty() {
// 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.
class ErrorHandler {
private $private;
function setup() {
function ($errno, $errstr, $errfile, $errline) {
$this->handleError($errno, $errstr, $errfile, $errline);
return true;
private function handleError($errno, $errstr, $errfile, $errline, $errmodule = null) {
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.
echo "Never executed in PHP7.2; there is a fatal error in the line above\n";
function reproduceBug(Foo $f) {
$exception = new Exception("Die you proof of concept");
$frames = $exception->getTrace();
// 1st stack frame contains the Foo object as 1st argument
$foo = $frames[0]['args'][0];
$reflect = new ReflectionObject($foo);
$properties = $reflect->getProperties();
foreach ($properties as $property) {
$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";
$errorHandler = new ErrorHandler();
reproduceBug(new Foo());

This comment has been minimized.

Copy link
Owner Author

juan-leon commented Sep 26, 2018

There is a simpler version showing the same issue here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.