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

PHP: Fixed methods with keyword names + minor improvements #2818

Merged
merged 5 commits into from
Apr 3, 2021

Conversation

RunDevelopment
Copy link
Member

This resolves #2817.


Apart from the obvious fix, I also made some minor improvements to the code.

@github-actions
Copy link

github-actions bot commented Mar 19, 2021

JS File Size Changes (gzipped)

A total of 1 files have changed, with a combined diff of +48 B (+2.5%).

file master pull size diff % diff
components/prism-php.min.js 1.9 KB 1.94 KB +48 B +2.5%

Generated by 🚫 dangerJS against 345cfef

@danon
Copy link
Contributor

danon commented Mar 23, 2021

@RunDevelopment Hey, perhaps I should've mentioned. Instance methods are called with -> operator, but static methods are called with :: operator. So I believe you need one pattern for function definition, one for instance method call and one for static method call.

Here's a Prism test page: https://prismjs.com/test.html#language=php&text=%24variable-%3Eforeach()%3B%20%2F%2F%20this%20%22foreach%22%20is%20a%20method%0A%24variable-%3Emethod()%3B%20%20%2F%2F%20this%20is%20also%20a%20method%0A%0Aforeach%20(%24list%20as%20%24value)%20%7B%20%2F%2F%20this%20%22foreach%22%20is%20a%20keyword%0A%7D%0A%0ATest%3A%3Aforeach()%3B%0ATest%3A%3Amethod()%3B%0A

$variable->foreach(); // this "foreach" is a method
$variable->method();  // this is also a method

foreach ($list as $value) { // this "foreach" is a keyword
}

Test::foreach();  // this is "foreach" static method
Test::method();  // this is "method" static method

# Exceptions
$variable->class(); // this "class" should be interpreted as "keyword"
Test::class; // This "class" should still be a keyword, just as it is now. Please, don't break it.

@RunDevelopment
Copy link
Member Author

Wait, so class is always a keyword? Docs says:

As of PHP 7.0.0 these keywords are allowed as property, constant, and method names of classes, interfaces and traits, except that class may not be used as constant name.

So class can't be a constant but you also listed a method named class under exceptions.

@danon
Copy link
Contributor

danon commented Mar 23, 2021

Wait, so class is always a keyword?

Yes, class is always a keyword.

So class can't be a constant but you also listed a method named class under exceptions.

I meant that since PHP 7.4, you can use all keywords as method names, with the exception of class which can't.

@RunDevelopment RunDevelopment merged commit 7e8cd40 into PrismJS:master Apr 3, 2021
@RunDevelopment RunDevelopment deleted the issue2817 branch April 3, 2021 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Keywords should be colored as methods, when used as methods.
2 participants