-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add support for ::class to constant() #6763
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
Conversation
Why not just use https://www.php.net/manual/en/function.get-class.php ? |
@drealecs @francislavoie Sure I could patch Twig (and I will), but my goal here is to make the language consistent. |
I have not checked whether the implementation handles those edge cases, but for sure tests are lacking:
And, of course, the implementation of the following is definitely missing:
And:
|
9555140
to
7471136
Compare
7471136
to
d68ba5d
Compare
Thanks for the feedback @claudepache! I also updated the implementation to not trigger autoloading and not complain about missing classes, but my patch introduces a memory leak (https://github.com/php/php-src/pull/6763/files#diff-6f9b1b4ec76510c10bf1c606e8629eb6b4d5f9df8f21258fd73a3e76bcab4796R384) and I don't know how to fix it. The problem is that for non-existing classes we must create a new
This is a good one! On the one hand, adding support in As a side effect, adding |
The larger question would be what to do abut magic constants, for example |
It's not possible to fix this without changing the API. You'd convert |
Thanks, @nikic! That's what I thought. An API change like this is probably too big to support such an edge case. |
Changing the API isn't really a big deal. It's not used much and largely for internal use. For reference, the internals discussion for this change: https://externals.io/message/113426 As the reception wasn't super positive, do you plan to pursue an RFC on the topic? |
Thanks for the explanation regarding how to change the API!
As there is no consensus on this and it provides only small benefits, no I don't think so. Let's close! |
This PR was merged into the 3.x branch. Discussion ---------- add support for the class constant on objects This is especially useful for Symfony Turbo: Before: ```twig <div id="books" {{ turbo_stream_listen('App\\Entity\\Book') }}></div> ``` After: ```twig <div id="books" {{ turbo_stream_listen(constant('class', book) }}></div> ``` Replace php/php-src#6763 Commits ------- 0d6bd45 add support for the class constant on objects
This change allows retrieving the value of the
::class
special constant using theconstant()
function:For instance, Twig's
constant()
helper internally uses this function. This patch allows writing code like that:TODO: write an RFC?