note 28828 added to language.oop
| From: | jp at antz29 dot com | Date: | Sat, 25 Jan 2003 04:17:28 +0000 |
| Subject: | note 28828 added to language.oop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-42716@lists.php.net to get a copy of this message | ||
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.
--
http://www.php.net/manual/en/language.oop.php
http://master.php.net/manage/user-notes.php?action=edit+28828
http://master.php.net/manage/user-notes.php?action=delete+28828
http://master.php.net/manage/user-notes.php?action=reject+28828