How to get the class name out of an incomplete class?
<p>We have an error handler that is throwing an error:
__PHP_Incomplete_Class could not be converted to string.</p>

<p>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.</p>

<pre><code>&lt;?php

$o = 'O:14:"BogusTestClass":0:{}';

$c = unserialize($o);

var_dump(
    array(
        'subject' =&gt; $c,
        'is_object' =&gt; is_object($c), // false !?!?
        'get_class' =&gt; get_class($c), // __PHP_Incomplete_Class
        'gettype' =&gt; gettype($c),     // 'object'
        'Is instance of?' =&gt; $c instanceof __PHP_Incomplete_Class, // true
        'serial' =&gt; serialize($c),
    )
);    

// Tried:

var_dump($c-&gt;__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-&gt;getProperty('__PHP_Incomplete_Class_Name');
$refProp-&gt;setAccessible(true);
var_dump($refProp-&gt;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] &amp;&amp; strlen($parts[2]) -2 == $parts[1]) {
        return substr($parts[2], 1, -1);
    }
    return '-- Error --';
}

var_dump(getBadClassName($c));
</code></pre>

<p>Attempting to get the name of the serialized class out of the incomplete object for use in an error message.</p>

<p>Avoiding parsing the string, because I'm guessing string parsing will break down when extensions that redefine serialize/unserialize are used, such as <a href="http://pecl.php.net/package/igbinary" rel="nofollow">http://pecl.php.net/package/igbinary</a> or <a href="http://pecl.php.net/package/APC/3.1.7" rel="nofollow">http://pecl.php.net/package/APC/3.1.7</a> apc.serializer hook.</p>
