Doc #64847 [NEW]: It appears that traits may define static member variables
| From: | mhorowitz at etsy dot com | Date: | Wed, 15 May 2013 18:49:28 +0000 |
| Subject: | Doc #64847 [NEW]: It appears that traits may define static member variables | ||
| Groups: | php.doc.bugs | ||
| Request: | Send a blank email to doc-bugs+get-9824@lists.php.net to get a copy of this message | ||
From: mhorowitz at etsy dot com
Operating system: linux
PHP version: 5.4.15
Package: Documentation problem
Bug Type: Documentation Problem
Bug description:It appears that traits may define static member variables
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 bug report at https://bugs.php.net/bug.php?id=64847&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=64847&r=trysnapshot54
Try a snapshot (PHP 5.3): https://bugs.php.net/fix.php?id=64847&r=trysnapshot53
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=64847&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=64847&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=64847&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=64847&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=64847&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=64847&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=64847&r=support
Expected behavior: https://bugs.php.net/fix.php?id=64847&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=64847&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=64847&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=64847&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=64847&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=64847&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=64847&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=64847&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=64847&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=64847&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=64847&r=mysqlcfg