Traits and static variables
| From: | Stefan Marr | Date: | Sat, 05 Jun 2010 20:47:34 +0000 |
| Subject: | Traits and static variables | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-48664@lists.php.net to get a copy of this message | ||
Hi:
Was just thinking about some details of the traits implementation.
From my perspective, static variables in methods should work like the method would have been
actually implemented in the class using the traits. Thus, static variables should be independent for
the different traits usages.
Any thoughts on that?
Below, I attached the relevant test case.
Best regards
Stefan
--TEST--
Statics work like expected for language-based copy'n'paste. No link between methods from
the same trait.
--FILE--
<?php
error_reporting(E_ALL);
trait Counter {
public function inc() {
static $c = 0;
$c = $c + 1;
echo "$c\n";
}
}
class C1 {
use Counter;
}
class C2 {
use Counter;
}
$o = new C1();
$o->inc();
$o->inc();
$p = new C2();
$p->inc();
$p->inc();
?>
--EXPECTF--
1
2
1
2