note 28828 deleted from language.oop by irchtml
| From: | irchtml@php.net | Date: | Sat, 28 Feb 2004 23:16:02 +0000 |
| Subject: | note 28828 deleted from language.oop by irchtml | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-65968@lists.php.net to get a copy of this message | ||
Note Submitter: jp@antz29.com
----
ok, found a simple way to implement static class variables. this method may be frowned upon by
anyone and everyone but it worked fine for me and is certainly more readable than 99% on
alternatives that i have looked into. see below.
class A {
#this is going to be my static class variable.
var $foo;
function A() {
#define a global for my static variable.
#prepend the class name to the var name for clarity.
global $A_foo;
#and refrence it to my class variable.
$this->foo =& $A_foo;
}
function setfoo($val) {
$this->foo = $val;
}
function getfoo() {
return $this->foo;
}
}
#create two instances of the A object.
$foobar1 = new A;
$foobar2 = new A;
#set the first object's foo to bar.
$foobar1->setfoo('bar');
print "FOO-1 = ".$foobar1->getfoo()."<br>";
print "FOO-2 = ".$foobar2->getfoo()."<br>";
/*
This outputs:
FOO-1 = bar
FOO-2 = bar
*/
#and just to make my point clear, set the second object's foo to foobar.
$foobar2->setfoo('foobar');
print "FOO-1 = ".$foobar1->getfoo()."<br>";
print "FOO-2 = ".$foobar2->getfoo()."<br>";
/*
This outputs:
FOO-1 = foobar
FOO-2 = foobar
*/
see ist't that fantastic? feel free to e-mail me with any comments/questions.