php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62156 Documentation incorrectly states that traits can't define static properties.
Submitted: 2012-05-25 11:45 UTC Modified: 2015-04-16 11:41 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: phplists at stanvassilev dot com Assigned: requinix (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: -
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: phplists at stanvassilev dot com
New email:
PHP Version: OS:

 

 [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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-22 21:17 UTC] 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)
 [2015-04-15 14:04 UTC] ppaisndud at gmail dot com
Documentation has been fixed can be closed
 [2015-04-16 11:41 UTC] requinix@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: requinix
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Apr 29 20:01:26 2025 UTC