Re: [RFC] Attributes v2
| From: | Mike Schinkel | Date: | Tue, 10 Mar 2020 01:01:57 +0000 |
| Subject: | Re: [RFC] Attributes v2 | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-108932@lists.php.net to get a copy of this message | ||
> On Mar 9, 2020, at 10:42 AM, Benjamin Eberlei <kontakt@beberlei.de> wrote:
>
> Hi all,
>
> 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 is at https://wiki.php.net/rfc/attributes_v2
>
> A working patch is at
> https://github.com/beberlei/php-src/pull/2 though
> work around the details is still necessary.
>
> 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.
>
> There is also a fair amount of implementation detail still up for debate,
> which is noted in "Open Issues". I have pre-committed to one approach, but
> listed alternatives there. On these issues I am looking for your feedback.
>
> greetings
> Benjamin
I am very excited to see this. I make heavy use of pseduo-attributes in both PhpDoc and using class
constants, and having real attributes would be quite the boon.
I do have a few concerns.
1. The syntax makes my eyes bleed.
---------------
I find angle brackets extremely hard to read and fear — have trained many newbies in programming
— that it will cause newbies who see PHP to think it is too complex for them to consider learning.
You mention that the good symbols are taken, but why limit ourselves to symbols? Why not use words
like PHP uses for other parts of the language?
So instead of:
<<SingleArgument("Hello")>>
<<Another\SingleArgument("World")>>
<<\My\Attributes\FewArguments("foo", "bar")>>
function foo() {}
Why not use?:
attribute SingleArgument("Hello")
attribute Another\SingleArgument("World")
attribute \My\Attributes\FewArguments("foo", "bar")
function foo() {}
If we don't want to make attribute a keyword, why not this?:
function attribute SingleArgument("Hello")
function attribute Another\SingleArgument("World")
function attribute \My\Attributes\FewArguments("foo", "bar")
function foo() {}
Or?:
function attributes
SingleArgument("Hello"),
Another\SingleArgument("World"),
\My\Attributes\FewArguments("foo", "bar")
function foo() {}
Alternately, why not use this (which is probably the best option IMO)?:
function foo() attributes
SingleArgument("Hello"),
Another\SingleArgument("World"),
\My\Attributes\FewArguments("foo", "bar") {}
2. How do your attributes relate to traits and interfaces?
---------------
I assume that anything you could do in a class you could do in a trait, but what about interfaces?
Using my syntax, could we have attributes defines in an interface and then require them to be
implemented? For example,
interface Storable attributes
StorageEngine(string $engine),
DoNotTestMethodsPrefixedWith(string $prefix){
function store() attributes PermissionsRequired(string $role) {}
}
When implemented that might look like this:
class AdminOnly() implements Storable attributes
StorageEngine("mongodb"),
DoNotTestMethodsPrefixedWith("_"){
function store() attributes PermissionsRequired("admin") {}
...
}
Other than those two concerns, I'm sold!
-Mike