Skip to content

Instantly share code, notes, and snippets.

@juan-leon
Created September 26, 2018 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juan-leon/cd52f9c9e7394d0897764fb2197e624b to your computer and use it in GitHub Desktop.
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
#!/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