<?php | |
function flattenExceptionBacktrace(\Exception $exception) { | |
$traceProperty = (new \ReflectionClass('Exception'))->getProperty('trace'); | |
$traceProperty->setAccessible(true); | |
$flatten = function(&$value, $key) { | |
if ($value instanceof \Closure) { | |
$closureReflection = new \ReflectionFunction($value); | |
$value = sprintf( | |
'(Closure at %s:%s)', | |
$closureReflection->getFileName(), | |
$closureReflection->getStartLine() | |
); | |
} elseif (is_object($value)) { | |
$value = sprintf('object(%s)', get_class($value)); | |
} elseif (is_resource($value)) { | |
$value = sprintf('resource(%s)', get_resource_type($value)); | |
} | |
}; | |
do { | |
$trace = $traceProperty->getValue($exception); | |
foreach($trace as &$call) { | |
array_walk_recursive($call['args'], $flatten); | |
} | |
$traceProperty->setValue($exception, $trace); | |
} while($exception = $exception->getPrevious()); | |
$traceProperty->setAccessible(false); | |
} |
This comment has been minimized.
This comment has been minimized.
Shouldn't it unset $call after the foreach-by-reference? |
This comment has been minimized.
This comment has been minimized.
I have found that with the way that PHP 7 handles errors now, treating them as exceptions, my error handlers receive Error type throwables too. As per PHP's reccomendation, we must now expect to receive Throwables, rather than just Exceptions. Unfortunately, while we can change the type hint to throwable, we cannot just change the reflection class type to Throwable as it does not contain the property trace, but if we check if the throwable is an instance of Error or Exception, we can then get the trace property as expected. If we do not do this, then any throwable type errors sent to this function will raise an exception that they are not an instance of the object type exception (in a more cryptic manner). For the PHP 7 ready modifications, see my fork. |
This comment has been minimized.
This comment has been minimized.
Be aware to use this function. function foo(&$bar)
{
flattenExceptionBacktrace(new \Exception());
}
$bar = new \DateTime();
foo($bar);
var_dump($bar); // outputs: string(16) "object(DateTime)" |
This comment has been minimized.
Demo: