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

constant() behaves inconsistent when class is undefined #9905

Closed
AMRoche opened this issue Nov 8, 2022 · 3 comments
Closed

constant() behaves inconsistent when class is undefined #9905

AMRoche opened this issue Nov 8, 2022 · 3 comments

Comments

@AMRoche
Copy link

AMRoche commented Nov 8, 2022

Description

The following code:

<?php

try {
    \constant("\ExistantClass::non_existant_constant");
} catch (\Throwable|\Error|\Exception $e) {
    echo($e->getMessage());
}

Resulted in this output:

⇒  php test.php
PHP Fatal error:  Class "ExistantClass" not found in /Users/alexroche/Desktop/test.php on line 4

Fatal error: Class "ExistantClass" not found in /Users/alexroche/Desktop/test.php on line 4

But I expected this output instead:

"Error : Class "ExistantClass" not found"

(i.e. the error to be caught and the message to be echo'd - what that message is is purely semantics)

PHP Version

8.0.25, 8.1.12

Operating System

No response

@cmb69
Copy link
Member

cmb69 commented Nov 8, 2022

If you are referring to a non-existant class, that results in a fatal error (E_ERROR). It is not possible to catch these, since they are no Errors. What you can do, however, is to check whether the class is available, and not even try to use it otherwise: https://3v4l.org/LQfZp

Anyhow, as is, this is not a bug. We could treat this as feature request to change the fatal error to an Error, but I'm not sure if that could be reasonably done.

@AMRoche
Copy link
Author

AMRoche commented Nov 8, 2022

Hi @cmb69 - Honestly, whatever y'all think is best is fine by me, but then at the very least the documentation for constant() is misleading (https://www.php.net/manual/en/function.constant) as it states an Error will be thrown if the constant doesn't exist (which logically if the class doesn't it won't), and that it also works with class constants.

In addition there's the following;

<?php

try {
    // does not work as i'd expected
    // constant("\NonExistantClass::constant");

    //does work as i'd expect
    //$temp = \NonExistantClass::constant;
    
    // new NonExistantClass();

    //$temp = "\NonExistantClass::constant";
    //eval($temp.";");

    //does what the old behavior of constant was
    //$temp = "\NonExistantClass::constant";
    //$$temp;
} catch (\Throwable|\Error|\Exception $e) {
    echo($e->getMessage());
}

@cmb69
Copy link
Member

cmb69 commented Nov 8, 2022

Hmm, given that other cases of undefined classes throw an Error, the constant() case might just have been missed.

cmb69 added a commit to cmb69/php-src that referenced this issue Nov 8, 2022
Directly referring to a constant of an undefined throws an exception;
there is not much point in `constant()` raising a fatal error in this
case.
@cmb69 cmb69 self-assigned this Nov 8, 2022
@cmb69 cmb69 changed the title constant() behaves unpredictably when class attached to constant is undefined constant() behaves inconsistent when class is undefined Nov 8, 2022
@cmb69 cmb69 closed this as completed in b2186ca Nov 9, 2022
cmb69 added a commit that referenced this issue Nov 9, 2022
* PHP-8.1:
  Fix GH-9905: constant() behaves inconsistent when class is undefined
cmb69 added a commit that referenced this issue Nov 9, 2022
* PHP-8.2:
  Fix GH-9905: constant() behaves inconsistent when class is undefined
fabpot added a commit to twigphp/Twig that referenced this issue Dec 26, 2022
…n first to avoid hard crash (Alex Henderson-Roche)

This PR was merged into the 2.x branch.

Discussion
----------

Updates CoreExtension::twig_constant to check for definition first to avoid hard crash

The behaviour of PHP's constant() method has been updated after php/php-src#9905 was accepted and fixed in PHP 8.1 (and previously changed post PHP 8.0) . This PR prevents a fatal error in the case a constant is supplied that doesn't actually exist.

Commits
-------

56b3122 Updates CoreExtension::twig_constant to check for definition first to avoid hard crash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@AMRoche @cmb69 and others