stijn0713 Posted April 7, 2013 Share Posted April 7, 2013 From the manual: "Static variables can be referred to in trait methods, but cannot be defined by the trait. Traits can, however, define static methods for the exhibiting class." trait Counter { public function inc() { static $c = 0; $c = $c + 1; echo "$c\n"; } } Static variables always have to be defined in methods if i'm right, and considering that the above example works perfectly, i don't see what would be a problem to work with static variables in traits. So my question is: What is meant with "but cannot be defined by the trait"? Quote Link to comment https://forums.phpfreaks.com/topic/276654-static-variables-in-traits/ Share on other sites More sharing options...
trq Posted April 8, 2013 Share Posted April 8, 2013 Static variables always have to be defined in methods if i'm right That is incorrect. You can define a static property like any other. Quote Link to comment https://forums.phpfreaks.com/topic/276654-static-variables-in-traits/#findComment-1423477 Share on other sites More sharing options...
requinix Posted April 8, 2013 Share Posted April 8, 2013 Meaning defined at the class (trait) level. Defining them in methods is fine. trait Counter { private static $c = 0; public function inc() { $this->c++; echo $this->c, "\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/276654-static-variables-in-traits/#findComment-1423543 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.