PHP struggles with attributes syntax
PHP 8.0 is on the horizon, and the project has imposed a feature freeze for the release. There's one exception to the feature freeze, though: the new attributes syntax. An attribute is syntactical metadata for PHP code, identical to what is called an "annotation" in other languages. Even though attributes have been voted on multiple times by the community, major contributor and creator of XDebug Derick Rethans threw a wrench into the works days before the feature freeze by challenging the current syntax. The ensuing discussion lead to the fourth attributes proposal for the year, with a special feature freeze exception being made by release manager Sara Golemon. This exception gives Rethans one more opportunity to convince the community to change how attributes work up to the Beta 3 release, scheduled for September 3.
What are PHP attributes?
As said, attributes are metadata for code, and provide a way to attach metadata to classes, functions, object properties, and so on to add information that isn't necessarily relevant to the execution of that code (such as the name of the author responsible for writing a particular method). While they can be used strictly for informational purposes, attributes can also play an important role in the code itself — such as telling the PHP engine if a particular method should be compiled by the just-in-time compiler also coming in PHP 8.0. Currently, the idea of attributes in PHP takes the form of a pseudo-language implemented as multi-line comments known as DocBlocks. In PHP 8.0, the hope is to replace the need for these DocBlocks with a native attribute syntax that can be parsed by the PHP engine. These attributes would then be available to the PHP engine itself, as well as being made available to developers through PHP's reflection capabilities.
The idea of attributes in PHP is not a new concept; contributor Dmitry Stogov proposed it back in 2016 for PHP 7.1. After extensive discussion, this original proposal was not accepted. The concept of attributes was recently brought back to life for PHP 8.0 by Benjamin Eberlei with a new proposal based on Stogov's work. Eberlei went through considerable effort, according to his post, to update things based on the feedback from Stogov's failed attempt, writing at the time:
I want to resurrect Dmitrys Attributes RFC that was rejected for 7.1 in 2016 with a few changes, incorporating feedback from the mailing list back then and from talking to previous no voters. [...] The RFC contains a section with common criticism and objections to attributes, and I hope to have collected and responded to a good amount already from previous discussions.
Eberlei was successful in getting his proposal accepted by a 51 to 1 vote in favor. Additionally, the PHP internals community voted on the syntax that these new attributes would use. They were asked to select between two choices: << >> and @:. Both choices allow for attributes to have function-like arguments that would also be parsed to be used in a yet-to-be-determined way. Here is an example of how these would look in code, taken from the proposal:
/* Using the << >> syntax */
<<WithoutArgument>>
<<SingleArgument(0)>>
<<FewArguments('Hello', 'World')>>
function foo() { /* ... */}
/* Using the @: syntax */
@:WithoutArgument
@:SingleArgument(0)
@:FewArguments('Hello', 'World')
function foo() { /* ... */
Ultimately, the community voted to accept the << >> syntax over @: in a 41 to 12 vote, and the matter was believed to be resolved.
The fight over syntax
Not all of the community was satisfied with the decision on syntax, however, and last month Theodore Brown (with Martin Schröder) published another proposal to change the syntax from the previously accepted << >> syntax to @@ or #[] as shown:
/* Using the @@ syntax */
@@FewArguments('Hello', 'World')
function foo() { /* ... */}
/* Using the #[] syntax */
#[FewArguments('Hello', 'World')]
Function foo() { /* ... */ }
In the proposal, Brown itemized several faults he found with the accepted attribute syntax, among them were: its verbosity, its inability to support nested attributes, its dissimilarity with other languages that implement similar constructs, and potential confusion with bit-shifting operations.
The proposal included a vote to first decide if a change should even be
considered, followed by a ranked-choice vote on the preference for the new
syntax. The options provided by the proposal for the new syntax were:
<< >> (the original), @@, or #[].
Brown's proposal passed with a 50 to 8 vote in favor of changing the syntax.
The top choice for the new syntax was @@, followed by <<
>>, and finally #[]. Brown's proposal succeeded in
changing the syntax to @@, and again the community moved forward,
believing the matter to be resolved. As of the first alpha versions of PHP 8.0, the @@ syntax for attributes remained — until Rethans
interjected. Rethans decided to throw the aforementioned wrench into the
works with an
email to internals on July 22 (less than two weeks before the PHP 8.0
feature freeze) entitled "The @@ is terrible, are we sure we're OK with
it?
":
I know we've voted twice on this already, but are we really sure that the @@ syntax is a good idea?
- There are lots of grumbles, both on here, room 11 [A Stack Exchange chat room], as well as in the wider community (https://www.reddit.com/r/PHP/comments/hjpu79/it_is/)
- It has the distinct possibility to cause further parsing issues, akin to what ended up happening with what Nikita is addressing at https://wiki.php.net/rfc/namespaced_names_as_token
- There is no "end symbol" to make finding [occurrences] easier.
- It is a syntax no other language uses.
- @ is never going to go away, so the possibility of @@ moving to @ is also 0.
Please, let's do the sensible and use the Rusty #[...] syntax.
In the discussion that followed, Brown pushed back against yet another vote on syntax and challenged Rethans's complaints:
Most of the comments in that Reddit thread appear to be positive or neutral, and in the earlier community poll, the @@ syntax received by far the most votes (https://www.reddit.com/r/PHP/comments/h06bra/community_poll_attribute_syntax/). So while some in the community may prefer a different syntax, this doesn't seem to reflect the majority.
If @@ actually has parsing issues, then I would agree that we need to pick another syntax. But there aren't any issues following Nikita's RFC to treat namespaced names as tokens. Please check out the implementation and let me know if you find any problems: https://github.com/php/php-src/pull/5796.
As for Rethans's suggestion to use the #[] syntax, Brown further pointed out that the proposal had already been made and rejected:
The benefit of #[] being exactly the same as another language was considered in the Shorter Attribute Syntax RFC, but apparently most voters didn't feel that this outweighed the downsides of a larger [backward compatibility] break, extra verbosity, and possible confusion with comments.
Ultimately, release manager for PHP 8.0, Sara Golemon,
weighed in on the matter and challenged Rethans to back up his claims
that the syntax would lead to "further parsing issues
". With PHP 8.0 feature freeze scheduled less than two weeks away, Golemon said she
needed "something more substantial than 'it maybe breaks something
somewhere somehow'
" to consider revisiting attribute syntax. Golemon
did seem to give Rethans the benefit of the doubt regarding these parser
issues, however, saying "I'm not doubting that there is one, you're
quite clever, but at the moment you're stating facts not currently in
evidence.
"
After what appears to have been conversations that took place outside of the regular internals mailing list, Golemon followed up on her post to share what she had learned regarding Rethans's supposed parser issues:
So evidently, this is specifically an issue with attributes in places where a portion of their name could be mistaken for a type. AIUI [As I understand it], that's being addressed.
[...] Regards the vote; I don't believe that @@ has been proven unworkable, however if I'm wrong about that, then the second choice selection from the last vote would obviously take precedence.
In the lengthy conversation that followed, which included Golemon suggesting that attributes be removed from PHP 8.0 and moved to PHP 8.1, she decided to give everyone time to sort out the mess:
I'm willing to extend an additional period (up to the tagging of beta3, in just under six weeks) for a re-vote on the syntax as changing that will be less violent of a change than merging the entire implementation after branching.
I hope this solution is both procedurally appropriate and satisfactory to all.
Where does that leave attributes?
The PHP 8.0 feature freeze happened as scheduled; in the meantime, Rethans (with the help of contributor Joe Ferguson) submitted yet another proposal for attribute syntax. According to Ferguson:
An important part of the research that went into this proposal demonstrates how '@@' will make handling attributes more difficult for PHP_CodeSniffer (and similar tools) and shows how the alternative '#[]' syntax is more friendly for these tools.
PHP_CodeSniffer is a static-analysis tool for PHP to detect and correct violations of coding standards within a PHP code base. As might be expected, Brown continued to argue against another change with support from others. In contrast, other longstanding members of the PHP internals community like Paul M. Jones seem to be uninterested with re-voting on the matter. In response to the new proposal, Jones wrote:
But if we are to make decisions by what is ostensibly a democratic process, we should stick to the voted decisions, instead of re-voting issues until the voters "get it right" according to some implicit and unstated criteria. (If re-voting over and over is the plan, I've got some RFCs I might like to revisit as well.)
Ultimately Eberlei, as the original proposer of the implementation of attributes that the community accepted, managed to find common ground between the parties by offering a rather complicated solution using the single transferable vote (STV) system:
As the author of the original RFC and patch, I hope I have some [clout] in suggesting the following procedure (RMs [release managers] would need to extend their approval for revote to this).
- we collect syntax proposals once again, with the requirement of a simple patch being available against php-src/master for viability in 8.0.
- RMs are the arbiter to decide the patch is acceptable to be included for 8.0 or if its selection would delay entire attributes to 8.1.
- I would make a feature matrix for the vote / RFC page and sort each proposed syntax into it, seeking input from the proposers.
- We would then hold another vote on syntax using STV where the choice is a combination of syntax and target version, examples:
- <<>> in 8.0, #[] in 8.0, @@ in 8.0 (all these patches are viable)
- @@ in doc blocks for 8.1
- §[] in 8.1 (weird example to demonstrate the point)
and so on. The STV vote would run with potentially 5-10 different syntaxes.
On Time Frame: Sara allowed to extend this decision into feature freeze, but I believe it shouldn't be later than Beta 2 (August 20th), especially if the outcome could be delay until 8.1.
Both Rethans and Ferguson agreed with Eberlei's suggestion and updated the proposal to reflect the proposed voting matrix. Meanwhile, the community is waiting for the release managers to make a decision on Eberlei's suggestion. With Rethans on board, it is unlikely the release managers will stand in the way. Unfortunately for everyone, however, the conflict over the syntax of attributes has left a significant language feature slated for PHP 8.0 up in the air for the time being. The general consensus is that no one in the community wants the feature to be delayed until PHP 8.1, but preventing that delay will require a meeting of the minds that has been elusive on this issue — only time will tell where things end up.
Posted Aug 7, 2020 0:27 UTC (Fri)
by Cyberax (✭ supporter ✭, #52523)
[Link] (9 responses)
Posted Aug 7, 2020 2:13 UTC (Fri)
by gus3 (guest, #61103)
[Link]
#!@*&#$%#$
Kidding aside, I agree with your point.
Nevertheless, it looks like a similar approach to when Guido stepped down from being the Python development leader. It's a case of "let the community figure it out, for better or worse, but then at least we'll be moving forward."
Posted Aug 7, 2020 5:09 UTC (Fri)
by flussence (guest, #85566)
[Link] (7 responses)
It became clear to me something wasn't right when namespaces were introduced; of all the possibilities and prior art out there, they instead went with *backslashes*.
Posted Aug 7, 2020 5:29 UTC (Fri)
by burki99 (subscriber, #17149)
[Link] (2 responses)
Posted Aug 8, 2020 19:45 UTC (Sat)
by smurf (subscriber, #17840)
[Link] (1 responses)
Posted Aug 13, 2020 11:03 UTC (Thu)
by flussence (guest, #85566)
[Link]
A subset of windows users at that. Japanese Win/DOS had ¥ everywhere \ was used. It's probably baked in as a hard alias for compatibility nowadays.
Posted Aug 7, 2020 6:11 UTC (Fri)
by ibukanov (subscriber, #3942)
[Link]
Posted Aug 8, 2020 5:43 UTC (Sat)
by iteratedlateralus (guest, #102183)
[Link] (2 responses)
Something like:
use App::Models::FooBar;
The current syntax, for those who aren't php devs, is:
use \App\Models\FooBar;
Posted Aug 8, 2020 21:21 UTC (Sat)
by jkingweb (subscriber, #113039)
[Link] (1 responses)
Posted Aug 9, 2020 6:57 UTC (Sun)
by iteratedlateralus (guest, #102183)
[Link]
Posted Aug 7, 2020 15:31 UTC (Fri)
by flewellyn (subscriber, #5047)
[Link] (7 responses)
Posted Aug 7, 2020 15:52 UTC (Fri)
by excors (subscriber, #95769)
[Link] (6 responses)
Posted Aug 7, 2020 20:08 UTC (Fri)
by Sesse (subscriber, #53779)
[Link] (3 responses)
Posted Aug 7, 2020 20:28 UTC (Fri)
by coogle (guest, #138507)
[Link] (2 responses)
Posted Aug 7, 2020 21:20 UTC (Fri)
by Sesse (subscriber, #53779)
[Link]
As for being a first cut; yes indeed, and a dead-end foundation is much worse than no foundation.
Posted Aug 8, 2020 5:45 UTC (Sat)
by iteratedlateralus (guest, #102183)
[Link]
Posted Aug 20, 2020 1:16 UTC (Thu)
by HelloWorld (guest, #56129)
[Link]
Posted Aug 20, 2020 1:17 UTC (Thu)
by HelloWorld (guest, #56129)
[Link]
Posted Aug 8, 2020 5:38 UTC (Sat)
by iteratedlateralus (guest, #102183)
[Link]
It's essentially doxygen, but the community very rarely deviates from the use of the '@' prefixed items within the comment. As a PHP dev, I'd like something that sort of meets in the middle. I think what PHP is doing is sort of "erasing" the bad taste that lots of people had when they saw how inefficient it was... Whatever they choose, I just hope it's not too much to invalidate the millions of doc blocks that already exist.
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
PHP struggles with attributes syntax
i.e.:
/**
* @param $foo
* @return App\Bar
*/
public function baz($foo) : App\Bar
{
...
}