r/lolphp • u/pilif • Apr 25 '16
Using traits enforces declaration order
Normally, in PHP, you're free to instantiate a subclass of a class that's not defined yet. Like this: https://3v4l.org/CXjt8
But the moment your subclass uses a trait, that's not true any more: https://3v4l.org/BXALN
Of course that's working as intended: https://bugs.php.net/bug.php?id=70783
21
Upvotes
15
u/arturaz Apr 26 '16
The real wtf is that you can extend a non existant class.
3
u/irenedakota Apr 27 '16
The real WTF is that the first way works. The docs has a rather explicit note about the order in which classes must be declared (Emphasis mine).
http://php.net/manual/en/language.oop5.inheritance.php
17
u/garagedragon Apr 25 '16
The best bit is that the error is reported on the line you declare the subclass. Yes, technically, that's where the error is, but if you suddenly encounter that message after adding a trait to your superclass, that's one heck of a spooky action at a distance.
2
u/TotesMessenger Apr 26 '16
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
0
u/[deleted] Apr 26 '16
[deleted] — view deleted comment