On Tue, Mar 10, 2020 at 9:41 PM Mike Schinkel <m...@newclarity.net> wrote:
> > On Mar 10, 2020 at 3:49 PM, <Benjamin Eberlei <kont...@beberlei.de>> > wrote: > > On Tue, Mar 10, 2020 at 5:30 PM Mike Schinkel <m...@newclarity.net> wrote: > >> > On Mar 10, 2020, at 11:28 AM, Benjamin Eberlei <kont...@beberlei.de> >> wrote: >> > >> > Just to make sure you don't run in circles in this discussion thread >> here, even when syntax is not fixed yet, it's not going to be a syntax >> where the attributes are suffixed after the declaration. It would maybe >> some other characters like %[Attr]. >> > >> >> It is your RFC so you are the arbiter. >> >> One final on syntax: Can I suggest you consider @:Attr? >> >> -Mike >> P. S. Did you see the question on interfaces? >> >> > No I missed it. Attributes work on interfaces, but they are not inherited > to parent classes. This is similar to how a class implementing an interface > may have different docblocks. You can use the reflection api to get to > interfaces from a class and check for their attributes there. For traits > the attributes get copied into the using class. > > > I think you may have misunderstood my question. I was asking if using an > interface we could *require* a class to implement specific attributes. If > yes that would be extremely valuable. > I think i have answered the question that it exactly works like doc blocks, meaning that an interface cannot require a subclass to "implement" a docblock and an attribute also cannot do that. In userland though, what you do with an interface requiring classes to implement attributes is up to you, so you can add this logic on top: interface ActiveRecord {} <<Table("bar")>> class Bar implements ActiveRecord {} class Baz implements ActiveRecord {} if ($object instanceof ActiveRecord) { $reflection = new ReflectionObject($object); $attributes = $reflection->getAttributes(Table::class); if (count ($attributes) == 0) { throw new \RuntimeException("Active Records are required to use the <<Table>> attribute"); } // .. } The logic you implement on top of attributes are entirely up to your imagination, attributes itself don't enforce anything (other than they must be a class that exists). > -Mike >