Timeline for Should a trait refer to parent methods?
Current License: CC BY-SA 4.0
5 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 29, 2018 at 16:08 | comment | added | Kamafeather | Yes your points make sense. I personally see the DeclareInInterface and AbstractInTrait as providing almost the same good (parse-time failing; interface/abstract fingerprint available, contrary to the reflection-way); but you are right underlining that interfaces is where one would usually go checking for methods to implement; I wouldn't go in a trait for that (and could also feel weird). I think the AbstractInTrait is a quick way to achieve the same result, when one doesn't want to think-about/maintain interfaces in the project. Btw I am trying with interfaces atm. | |
| Aug 21, 2018 at 18:58 | comment | added | zquintana | @Kamafeather what your describing is true, but I'd say interfaces are a better fit for that. They would be the best approach and you see other well established projects using that approach. An interface that describes some behavior and a trait that can be used to implement it in a portable fashion. IMO I think that's better then having a trait with an abstract method and using that abstract method as validation. IMO it's better when the calling code says "if interface implemented then" use that interface methods. It ensures at runtime that it's used properly without reflection. | |
| Aug 20, 2018 at 17:22 | comment | added | Kamafeather | @zquintana If I am not wrong amon's approach would ensure that the code will fail before running, in case the parent class didn't already define that abstract method. In a way it is a good (automatic) alternative to using reflection and checking in-code that the method exists (or throw an exception otherwise). It doesn't remove the potential smell of the intention, but at least this latter is explicit and allows to still use traits consistently in such case (especially when some already present structure/organization/hierarchy is already present and not changeable) 🙂 | |
| May 29, 2018 at 15:25 | comment | added | zquintana | @amom interesting approach, I've never seen that before, but it looks like it's valid in PHP. I can see how this could be valuable. That said the question was in reference to calling parent methods from a trait. I think that's where the code smell exists in the scenario posted in the question. | |
| May 18, 2018 at 20:56 | history | answered | amon | CC BY-SA 4.0 |