PHP __clone() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Exception

Definition

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
CallWhat it does
clone $eAlways 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

ParameterDescription
—This method takes no parameters and is not callable from user code.

Return Value

Return ValueDescription
voidNot 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.

PHP
<?php
$e = new Exception("Cannot copy me");

try {
    $copy = clone $e;
    echo "Cloned", PHP_EOL;
} catch (Error $err) {
    echo "Clone failed", PHP_EOL;
    echo $err->getMessage(), PHP_EOL;
}

How It Works

  1. An Exception instance is created normally.
  2. clone $e triggers Exception::__clone() protection.
  3. PHP throws an Error instead of copying the object.
  4. catch (Error $err) reports the failure safely.

Example 2 — Subclass Still Cannot Be Cloned

Show that extending Exception does not make cloning allowed.

PHP
<?php
class DemoException extends Exception {}

$e = new DemoException("Still locked");

try {
    clone $e;
} catch (Error $err) {
    echo get_class($e), " is not cloneable", PHP_EOL;
}

How It Works

  1. DemoException extends Exception.
  2. The private final __clone() behavior still applies.
  3. clone raises an Error.
  4. Custom exceptions remain non-cloneable.

Example 3 — Replace Clone with a New Exception

When you need a similar exception, construct a new one and optionally chain the old object.

PHP
<?php
$original = new Exception("Root cause", 10);
$wrapped = new Exception("Outer failure", 20, $original);

echo $wrapped->getMessage(), PHP_EOL;
echo $wrapped->getPrevious()->getCode(), PHP_EOL;

How It Works

  1. Instead of clone, we call new Exception(. ..).
  2. The original becomes the previous Throwable.
  3. getMessage() returns the new outer text.
  4. getPrevious()->getCode() still reaches the root code 10.

Frequently Asked Questions

No. Exception::__clone() is final and private, so clone $e raises an Error.
Exceptions represent a specific failure event. PHP blocks cloning so you rethrow or wrap them instead of copying them silently.
No. Declaring __clone() in a subclass does not remove the restriction for Throwable objects.
Rethrow the same instance, or create a new Exception and pass the old one as the previous argument.
No. Beginners mainly use throw, catch, getMessage(), and related getters. __clone() only matters when someone tries to copy an exception.

Back to Exception Methods

Browse all PHP Exception method tutorials.

Exception methods →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful