In PHP, Exception::__clone() exists to stop you from cloning exception objects. The method is final and private, so exceptions are not cloneable.
If you write clone $e on an Exception, PHP raises an Error. Create a new Exception with the same message and code when you need a similar object, or rethrow the original instance.
Syntax
Call __clone() with the values you need. Start with this line.
PHP
// clone $e; // Error: trying to clone an uncloneable object
Call
What it does
clone $e
Always fails for Exception; PHP raises an Error.
new Exception($e->getMessage(), $e->getCode(), $e)
Builds a fresh exception and can chain the old one as previous.
throw $e;
Rethrows the same instance instead of cloning it.
__clone() is final and private on Exception. You cannot override it to make exceptions cloneable. Attempting clone on an Exception results in an Error.
Library declarations
PHP
final private Exception::__clone(): void
Parameters
Parameter
Description
—
This method takes no parameters and is not callable from user code.
Return Value
Return Value
Description
void
Not used successfully from user code. Cloning an Exception fails with an Error before a normal return.
PHP Examples
Example 1 — Cloning an Exception Fails
Catch the Error that PHP throws when you attempt to clone an Exception.