Doc #64847 [Opn->Csd]: It appears that traits may define static member variables
| From: | krakjoe@php.net | Date: | Sun, 13 Oct 2013 17:40:04 +0000 |
| Subject: | Doc #64847 [Opn->Csd]: It appears that traits may define static member variables | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-10483@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64847&edit=1
ID: 64847
Updated by: krakjoe@php.net
Reported by: mhorowitz at etsy dot com
Summary: It appears that traits may define static member
variables
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: Documentation problem
Operating System: linux
PHP Version: 5.4.15
-Assigned To:
+Assigned To: krakjoe
Block user comment: N
Private report: N
New Comment:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation better.
Previous Comments:
------------------------------------------------------------------------
[2013-10-13 17:40:02] krakjoe@php.net
Automatic comment from SVN on behalf of krakjoe
Revision: http://svn.php.net/viewvc/?view=revision&revision=331806
Log: #64847 correction
------------------------------------------------------------------------
[2013-05-15 18:49:28] mhorowitz at etsy dot com
Description:
------------
Try it -- put a static variable in a trait declaration, then access that variable
in code defined in the trait and/or defined in the "exhibiting" class. It works
just fine.
I spoke with Rasmus; we both believe this is fine behavior and that the
documentation (quoted below) should be changed to make it clear that one can, in
fact, declare a static variable within the trait definition.
Quote from: http://php.net/manual/en/language.oop5.traits.php
"Static variables can be referred to in trait methods, but cannot be defined by
the trait."
Test script:
---------------
<?php
// So, the following works and, for my purposes, is really, really useful.
trait TTrait {
protected static $instance = 'hello there';
public function publicFn() {
echo self::$instance . "\n";
echo "publicFn\n";
}
}
class Foo {
use TTrait;
public function tryMe() {
echo "From Foo: "; $this->publicFn();
echo "In tryMe -- instance is: " . self::$instance . "\n";
}
}
$f = new Foo();
$f->tryMe();
Expected result:
----------------
As noted, please adjust the doc to reflect that static variable declarations are
actually allowed. Currently, name collisions are handled as is done for non-
static properties (i.e. E_STRICT error, as per the doc for properties).
In the future, it might be nice if the renaming clause would work for static
variables too.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64847&edit=1