[PHP-DEV] 5.4a2 trait attribute name conflict resolution
Hello folks, I've just grabbed 5.4a2 to play with traits. I've found some behaviour which I'm not sure is a bug, an inconsistency, or a design decision. Consider a trait and a class that implements it but also overrides both a trait method and a trait attribute: trait foo { public $zoo = 'foo::zoo'; public function bar() { echo in foo::bar\n; } } class baz { use foo; public $zoo = 'baz::zoo'; public function bar() { echo in baz::bar\n; } } $obj = new baz(); $obj-bar(); echo $obj-zoo, \n; We get: in baz::bar foo::zoo It seems this is not correct and that it should be: in baz::bar baz::zoo The traits RFC pretty clearly states that if a class method conflicts with a trait method then the trait method will be ignored, which is what's happening, but it says nothing about what happens to attributes in that same condition. Is this a bug? Thanks, -- Alex Howansky smime.p7s Description: S/MIME Cryptographic Signature
Re: [PHP-DEV] 5.4a2 trait attribute name conflict resolution
That makes sense if it would overwrite the methods as well, but otherwise it seems like it provides inconsistent functionality. Exactly. At the least, it's inconsistent. If it's a bug, then it seems the question becomes: Is the bug this: Properties defined in a trait should be overridden by same-named properties defined in a class that use the trait. Or (as pointed out by Anthony) this: You shouldn't be able to define properties in a trait. -- Alex Howansky smime.p7s Description: S/MIME Cryptographic Signature
Re: [PHP-DEV] 5.4a2 trait attribute name conflict resolution
Just curious, if the trait property is set to private what happens? Ooh, good question. PHP Fatal error: baz and foo define the same property ($zoo) in the composition of baz. However, the definition differs and is considered incompatible. -- Alex Howansky smime.p7s Description: S/MIME Cryptographic Signature
Re: [PHP-DEV] 5.4a2 trait attribute name conflict resolution
Best practice, always choose trait property names carefully/~unique so that you don't run into conflicts. Sure, but in this case, I created the conflict intentionally because I *want* to override it, and I'm not allowed to like I am with methods. Don't you think that's inconsistent? The short answer is it's not a bug but maybe an implementation issue... should it be an E_WARNING instead of E_STRICT? At least. Consider the situation where I'm using classes/traits from somebody else's library that I may not be intimately familiar with. I'll have to know what every one of their properties is named so I can plan my code accordingly -- else I'll silently start getting their values in what I think are my variables. -- Alex Howansky smime.p7s Description: S/MIME Cryptographic Signature