note 21047 added to language.references.pass
| From: | thomas at horsten dot com | Date: | Fri, 26 Apr 2002 15:17:49 +0000 |
| Subject: | note 21047 added to language.references.pass | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-29879@lists.php.net to get a copy of this message | ||
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 :)
--
http://www.php.net/manual/en/language.references.pass.php
http://master.php.net/manage/user-notes.php?action=edit+21047
http://master.php.net/manage/user-notes.php?action=delete+21047
http://master.php.net/manage/user-notes.php?action=reject+21047