note 21047 deleted from language.references.pass by sniper
| From: | sniper@php.net | Date: | Tue, 30 Sep 2003 01:24:45 +0000 |
| Subject: | note 21047 deleted from language.references.pass by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-57341@lists.php.net to get a copy of this message | ||
Note Submitter: thomas@horsten.com
----
Unfortunately static member vars are not supported, and declaring a static in a member function only
allows access from that function. But you can still get the effect of static member vars by using a
trick with returning a static by reference from a "container function", like this(notice
the & in function getStaticMember and the & in the call to the function as well, both are
required):
class TestXX {
function &getStaticMember() {
static $mystat = 2;
$v = &$mystat;
return $v;
}
function test1() {
// Access the static member:
$static_member = &$this->getStaticMember();
$static_member++;
echo "The static value is: " . $static_member . "<br>";
}
}
$t1 = new TestXX;
$t2 = new TestXX;
$t1->test1();
$t1->test1();
$t2->test1();
When you run this you'll get:
The static value is: 3
The static value is: 4
The static value is: 5
So now you have, in effect, a static member variable :)