Bug #62156 [Com]: Documentation incorrectly states that traits can't define static properties.
| From: | ppaisndud at gmail dot com | Date: | Wed, 15 Apr 2015 14:04:41 +0000 |
| Subject: | Bug #62156 [Com]: Documentation incorrectly states that traits can't define static properties. | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-12157@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62156&edit=1
ID: 62156
Comment by: ppaisndud at gmail dot com
Reported by: phplists at stanvassilev dot com
Summary: Documentation incorrectly states that traits can't
define static properties.
Status: Open
Type: Bug
Package: Documentation problem
Operating System: -
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Documentation has been fixed can be closed
Previous Comments:
------------------------------------------------------------------------
[2012-12-22 21:17:44] guillaumemessier at gmail dot com
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)
------------------------------------------------------------------------
[2012-05-25 11:45:36] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62156&edit=1