|
|
Subscribe / Log in / New account

PHP struggles with attributes syntax

By John Coggeshall
August 6, 2020

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?

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

  1. we collect syntax proposals once again, with the requirement of a simple patch being available against php-src/master for viability in 8.0.
  2. 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.
  3. I would make a feature matrix for the vote / RFC page and sort each proposed syntax into it, seeking input from the proposers.
  4. 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.



to post comments

PHP struggles with attributes syntax

Posted Aug 7, 2020 0:27 UTC (Fri) by Cyberax (✭ supporter ✭, #52523) [Link] (9 responses)

I suggest using "#!@*&#$#$" as the marker. Cause why the hell not?

PHP struggles with attributes syntax

Posted Aug 7, 2020 2:13 UTC (Fri) by gus3 (guest, #61103) [Link]

Because you're missing a percent sign. It should be

#!@*&#$%#$

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

PHP struggles with attributes syntax

Posted Aug 7, 2020 5:09 UTC (Fri) by flussence (guest, #85566) [Link] (7 responses)

Every syntax addition to PHP since 2005 feels like it's been forced by either a legacy design decision to use an ad-hoc giant switch statement parser for speed on CGI hosting, or an aversion to doing anything remotely risky for long-term benefit.

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

PHP struggles with attributes syntax

Posted Aug 7, 2020 5:29 UTC (Fri) by burki99 (subscriber, #17149) [Link] (2 responses)

I was originally also very irritated by the backslashes. About 10 years later, everybody is using them, and together with composer, they are huge success story. So I assume the same will happen with attributes, once the syntax is settled, people will get used to them, no matter which variant will be accepted.

PHP struggles with attributes syntax

Posted Aug 8, 2020 19:45 UTC (Sat) by smurf (subscriber, #17840) [Link] (1 responses)

Yeah, but that would have been a success with any other reasonable character. And, well, a backslash is only reasonable if you've been socialized on Windows. On Unix its connotation with "escape whatever special meaning the next character has" is too strong for me to feel comfortable with the thing.

PHP struggles with attributes syntax

Posted Aug 13, 2020 11:03 UTC (Thu) by flussence (guest, #85566) [Link]

> And, well, a backslash is only reasonable if you've been socialized on Windows.

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.

PHP struggles with attributes syntax

Posted Aug 7, 2020 6:11 UTC (Fri) by ibukanov (subscriber, #3942) [Link]

I also found the backslashes felt as not right. Backslashes in Haskell did not felt that way because the rest of language is so different. But foo\bar was so foreign in a language based on C syntax. Yet as a maintainer of a small website using PHP I strongly appreciate that the code written sensibly 15 years ago still works as is.

PHP struggles with attributes syntax

Posted Aug 8, 2020 5:43 UTC (Sat) by iteratedlateralus (guest, #102183) [Link] (2 responses)

The backslashes definitely weren't something I totally agreed with. I recall being in #php on Freenode, and saying that the backslash was too much of a shock (especially since it's an escape sequence). I said it would be use if they instead used the :: (the infamous php operator that I can't pronounce).

Something like:

use App::Models::FooBar;

The current syntax, for those who aren't php devs, is:

use \App\Models\FooBar;

PHP struggles with attributes syntax

Posted Aug 8, 2020 21:21 UTC (Sat) by jkingweb (subscriber, #113039) [Link] (1 responses)

While you probably know this now, that would have been ambiguous. Is ::foo:::bar::baz() a a call to a function in the ::foo::bar namespace, or to a method of class bar in the ::foo namespace?

PHP struggles with attributes syntax

Posted Aug 9, 2020 6:57 UTC (Sun) by iteratedlateralus (guest, #102183) [Link]

That's very true. Had it not been the static access operator, it might work.

PHP struggles with attributes syntax

Posted Aug 7, 2020 15:31 UTC (Fri) by flewellyn (subscriber, #5047) [Link] (7 responses)

They should really paint the bike shed green.

PHP struggles with attributes syntax

Posted Aug 7, 2020 15:52 UTC (Fri) by excors (subscriber, #95769) [Link] (6 responses)

Bikeshedding is when you spend a lot of time discussing a trivial problem and ignoring a big, complex, important problem (like the nuclear power plant that the bikeshed belongs to). When designing a programming language, I don't think syntax discussion can ever be bikeshedding: syntax *is* one of the big, complex, important problems in language design. Getting it wrong can have a major impact on readability, maintainability, learnability, aesthetic appeal, etc, and can create whole classes of bugs that simply don't exist in languages with better syntax, and can influence the design of every other feature that is later added to the language. It deserves a lot of discussion and careful thought over every detail.

PHP struggles with attributes syntax

Posted Aug 7, 2020 20:08 UTC (Fri) by Sesse (subscriber, #53779) [Link] (3 responses)

I would say arguing over attributes syntax while just brushing through a JIT that helps 3.5% on Wordpress fits your description quite well, really. (Said JIT vote was 50–2.)

PHP struggles with attributes syntax

Posted Aug 7, 2020 20:28 UTC (Fri) by coogle (guest, #138507) [Link] (2 responses)

I'd be interested in a link to the data behind the 3.5% improvement of WordPress with JIT. When it comes to JIT for PHP in general though, I think it's important to remember the RFC itself described it as pretty simplistic JIT and way far behind other JIT implementations for other languages. It's a first cut, a foundational effort. I'm certain over time past 8.0 it will continue to be improved and hopefully have a more dramatic impact on key PHP codebases like WordPress.

PHP struggles with attributes syntax

Posted Aug 7, 2020 21:20 UTC (Fri) by Sesse (subscriber, #53779) [Link]

The 3.5% number is on the RFC page: https://wiki.php.net/rfc/jit

As for being a first cut; yes indeed, and a dead-end foundation is much worse than no foundation.

PHP struggles with attributes syntax

Posted Aug 8, 2020 5:45 UTC (Sat) by iteratedlateralus (guest, #102183) [Link]

In my experience, Wordpress suffers from two things: 1) PHP Devs notoriously dislike it, and 2) The hooks system is potentially very frustrating to work with.

PHP struggles with attributes syntax

Posted Aug 20, 2020 1:16 UTC (Thu) by HelloWorld (guest, #56129) [Link]

That's just wrong. Syntax very much *is* the bike shed, because the important part is semantics.

PHP struggles with attributes syntax

Posted Aug 20, 2020 1:17 UTC (Thu) by HelloWorld (guest, #56129) [Link]

I can guarantee you that no software project in the world ever failed because of this kind of trivial syntactic differences.

PHP struggles with attributes syntax

Posted Aug 8, 2020 5:38 UTC (Sat) by iteratedlateralus (guest, #102183) [Link]

The current pattern for adding meta data to PHP code is to essentially use the popular docblocks...
i.e.:
/**
* @param $foo
* @return App\Bar
*/
public function baz($foo) : App\Bar
{
...
}

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.


Copyright © 2020, Eklektix, Inc.
This article may be redistributed under the terms of the Creative Commons CC BY-SA 4.0 license
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds