Edit report at https://bugs.php.net/bug.php?id=65111&edit=1
ID: 65111 Updated by: fel...@php.net Reported by: ryan dot brothers at gmail dot com Summary: Calling traits directly with static properties/methods -Status: Assigned +Status: Not a bug Type: Bug Package: Class/Object related Operating System: Linux PHP Version: 5.5.0 -Assigned To: gron +Assigned To: Block user comment: N Private report: N New Comment: Thanks for explaining it. Previous Comments: ------------------------------------------------------------------------ [2013-06-25 07:19:04] g...@php.net Yes, traits are not supposed to be instantiated. However, this behavior is not really related to instantiation. Traits are still a lexical entity, i.e., a programming language entity that provides a lexical scope when defined. Since PHP allows us to define functions in such scopes, we can define also static methods on traits. The way to avoid that would be to change the grammar for traits. I decided to not do that back in the day. And I still feel that it is unclear whether it would be conceptually cleaner if it is not possible to defined static state/methods. So, I would leave it as it is. Except, someone comes up with a good reason to change it. But it would also be quite a bit of a BC issue. ------------------------------------------------------------------------ [2013-06-25 04:30:41] larue...@php.net hmm, trait is actually a class in php, I have a quick look into this.. changing this will needs a significant works.. actually, the is_callable / do_call alreay is a little mess now. I mean the codes ------------------------------------------------------------------------ [2013-06-24 16:52:52] ryan dot brothers at gmail dot com Description: ------------ The documentation for traits indicates that "it is not possible to instantiate a Trait on its own", but I have noticed that a trait can be called directly if it has a static property or method, as per the below example. Is this intended behavior? Or should traits be restricted from being called directly? I was under the impression that traits cannot be called directly per the documentation. If not, is there a way to prevent traits from being called directly as in the below example? Test script: --------------- <?php trait t1 { public static $a1 = 'test1'; public static function test() { return 'test2'; } } echo t1::$a1."\n"; echo t1::test()."\n"; Expected result: ---------------- Fatal Error that traits cannot be called directly. Actual result: -------------- test1 test2 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=65111&edit=1