We have an error handler that is throwing an error: __PHP_Incomplete_Class could not be converted to string.
The error handler currently does an 'is_object' test, which oddly is false for an 'Incomplete' object. I'm attempting to adjusting the error handler, but I'm not able to get the original class name out, without parsing the re-serialized version.
<?php
$o = 'O:14:"BogusTestClass":0:{}';
$c = unserialize($o);
var_dump(
array(
'subject' => $c,
'is_object' => is_object($c), // false !?!?
'get_class' => get_class($c), // __PHP_Incomplete_Class
'gettype' => gettype($c), // 'object'
'Is instance of?' => $c instanceof __PHP_Incomplete_Class, // true
'serial' => serialize($c),
)
);
// Tried:
var_dump($c->__PHP_Incomplete_Class_Name);
// Error:
// The script tried to execute a method or access a property of
// an incomplete object
$refObj = new ReflectionObject($c);
$refProp = $refObj->getProperty('__PHP_Incomplete_Class_Name');
$refProp->setAccessible(true);
var_dump($refProp->getValue($c));
// Error:
// ReflectionProperty::getValue(): The script tried to execute a
// method or access a property of an incomplete object.
// This works, but is fragile, since it depends on internal behavior
// of serialize
function getBadClassName($subject)
{
$serial = serialize($subject);
$parts = explode(':', $serial, 4);
if ('O' === $parts[0] && strlen($parts[2]) -2 == $parts[1]) {
return substr($parts[2], 1, -1);
}
return '-- Error --';
}
var_dump(getBadClassName($c));
Attempting to get the name of the serialized class out of the incomplete object for use in an error message.
Avoiding parsing the string, because I'm guessing string parsing will break down when extensions that redefine serialize/unserialize are used, such as http://pecl.php.net/package/igbinary or http://pecl.php.net/package/APC/3.1.7 apc.serializer hook.