Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it clearer that Exceptions cannot be cloned #3418

Open
SigmaFlame opened this issue May 27, 2024 · 6 comments
Open

Make it clearer that Exceptions cannot be cloned #3418

SigmaFlame opened this issue May 27, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@SigmaFlame
Copy link

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 SigmaFlame added bug Documentation contains incorrect information Status: Needs Triage labels May 27, 2024
@iluuu1994
Copy link
Member

iluuu1994 commented May 27, 2024

Hi @SigmaFlame. See php/php-src#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 iluuu1994 transferred this issue from php/php-src May 27, 2024
@Girgias
Copy link
Member

Girgias commented May 27, 2024

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

@Girgias Girgias added enhancement New feature or request and removed bug Documentation contains incorrect information labels May 27, 2024
@Girgias Girgias changed the title Exceptions are still not clonable even if Exception::__clone() is no longer final Make it clearer that Exceptions cannot be cloned May 27, 2024
@iluuu1994
Copy link
Member

iluuu1994 commented May 27, 2024

@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.

@Girgias
Copy link
Member

Girgias commented May 27, 2024

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.

@SigmaFlame
Copy link
Author

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

@iluuu1994
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants