Open
Description
-
you can define a constructor in a trait
-
trait T implements IFoo and trait T { require implements IFoo; } are both legal, but have slightly different meanings
-
You can call T::someStaticMethod() directly today (but this should hopefully be banned soon)
-
You can define abstract methods in traits
-
You can use __ConsistentConstruct on a trait, and it's different to a class
Activity
edwinsmith commentedon Jul 9, 2020
class C{use T1,T2}
, if T1+T2 define conflicting abstract methods, T1 wins... but if either T1 or T2 has a concrete method, that one wins. finally, if C has the method as well, C wins.what are the two meanings?
what happens if
someStaticMethod()
contains language constructs (e.g.parent
or access to static properties) that only work in the context of a using class?edwinsmith commentedon Jul 9, 2020
HH typechecks this with override semantics - C's concrete method must have a compatible signature with the private method in T. hhvm doesn't care (T's private method is simply hidden).
edwinsmith commentedon Jul 10, 2020
__CLASS__
in a trait is rewritten to the class using the trait.Wilfred commentedon Jul 10, 2020
It throws an exception/fatals. This is why we need to ban it :)
Wilfred commentedon Jul 10, 2020
Given two traits:
Using them:
ExampleOne
is required to writeimplements IHasFoo
, whereasExampleTwo
implicitly getsimplements IHasFoo
from using the trait.https://fburl.com/1810mh57 has some additional discussion.
edwinsmith commentedon Jul 11, 2020
IIUC, both ExampleOne and ExampleTwo end up identical -- they both have the same bases, interfaces, and methods, correct? That said, I agree there is value to only allowing ExampleOne style.
Wilfred commentedon Jul 13, 2020
Yep, the only difference is that you're required to write
implements IHasFoo
forExampleOne
. At runtime they should be the same.