doc-en icon indicating copy to clipboard operation
doc-en copied to clipboard

Make it clearer that Exceptions cannot be cloned

Open SigmaFlame opened this issue 1 year ago • 6 comments

Description

Hello,

While reading the PHP documentation, I have read that the method Exception::__clone() is no longer final since PHP 8.1. But, trying to extend the class Exception and making its own « public function __clone() » does not work and still produce an error.

« Trying to clone an uncloneable object of class ExtendedException »

What is the purpose of removing the final attribute of this method if we still can't clone an exception? I would like to make the exception clonable in order to update the file and line where the exception has occurred.

Thanks you in advance.

PHP Version

8.3.7

Operating System

No response

SigmaFlame avatar May 27 '24 23:05 SigmaFlame

Hi @SigmaFlame. See https://github.com/php/php-src/pull/6892, the final modifier was removed not because exceptions are now clonable, but because private final is not a thing (as there's no way to override a final method). Maybe we can improve this message.

iluuu1994 avatar May 27 '24 23:05 iluuu1994

There already is a note about not being able to clone exceptions: https://www.php.net/manual/en/language.exceptions.extending.php

Girgias avatar May 27 '24 23:05 Girgias

@Girgias Indeed. I think this is referencing:

https://www.php.net/manual/en/exception.clone.php

Exception::__clone() is no longer final.

Without more context, this sounds like exceptions may now be cloneable. But they are not, even though they allow defining the __clone method, which is not great. It would be better if __clone was forbidden altogether, but yeah.

iluuu1994 avatar May 27 '24 23:05 iluuu1994

I think removing the changelog is probably sensible, and wrapping one of the "Exceptions are not clonable" in a warning tag so that it stands out.

Girgias avatar May 27 '24 23:05 Girgias

But why exceptions could not be cloned in the first place?

SigmaFlame avatar May 28 '24 15:05 SigmaFlame

Honestly, I have no idea what the rationale is for making exceptions unclonable. But it's been that way forever.

iluuu1994 avatar May 28 '24 21:05 iluuu1994