|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-05-25 11:45 UTC] phplists at stanvassilev dot com
Description: ------------ The Traits page at http://us.php.net/manual/en/language.oop5.traits.php states the following: "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." This is followed by an example of emulating static properties with function-static vars. In testing PHP 5.4 this is wrong, traits support static properties, and they work as expected... Test script: --------------- <?php trait T { static public $foo = 123; } class C { use T; } echo C::$foo; // 123 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 08:00:02 2025 UTC |
Php 5.4 seems to allow the definition of a static variable in a trait. It seems that a static variable is created for the trait itself and one for each class using the trait. <?php trait T { static public $var = 1; } class A { use T; } class B { use T; } var_dump(T::$var); T::$var = 2; var_dump(A::$var); A::$var = 3; var_dump(B::$var); B::$var = 4; var_dump(T::$var); var_dump(A::$var); var_dump(B::$var); ?> Return: int(1) int(1) int(1) int(2) int(3) int(4)