Bug #62156 [Opn->Csd]: Documentation incorrectly states that traits can't define static properties.

From: Date: Thu, 16 Apr 2015 11:41:52 +0000
Subject: Bug #62156 [Opn->Csd]: 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-12171@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
 Updated by:         requinix@php.net
 Reported by:        phplists at stanvassilev dot com
 Summary:            Documentation incorrectly states that traits can't
                     define static properties.
-Status:             Open
+Status:             Closed
 Type:               Bug
 Package:            Documentation problem
 Operating System:   -
 PHP Version:        Irrelevant
-Assigned To:        
+Assigned To:        requinix
 Block user comment: N
 Private report:     N



Previous Comments:
------------------------------------------------------------------------
[2015-04-15 14:04:40] ppaisndud at gmail dot com

Documentation has been fixed can be closed

------------------------------------------------------------------------
[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


Thread (4 messages)

« previous php.doc.bugs (#12171) next »