doc-en
doc-en copied to clipboard
Make it clearer that Exceptions cannot be cloned
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
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.
There already is a note about not being able to clone exceptions: https://www.php.net/manual/en/language.exceptions.extending.php
@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.
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.
But why exceptions could not be cloned in the first place?
Honestly, I have no idea what the rationale is for making exceptions unclonable. But it's been that way forever.