note 25622 deleted from language.oop.newref by danbrown

From: Date: Sat, 07 Nov 2009 19:11:19 +0000
Subject: note 25622 deleted from language.oop.newref by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162590@lists.php.net to get a copy of this message
Note Submitter: rick_php_net at roundeye dot net ---- I was attempting to find a way to share data structures between objects and ideally wanted to be able to have a single shared object that would be returned as a reference from the constructor -- without having the caller use a different syntax. I couldn't get the effect I wanted, but was able to share the internal data structures (which is where the bulk of the storage was in my application). This is an example class I constructed (requires php >= 4.0.4) which illustrates the idea: class StaticList { var $list = 0; function StaticList($name = '') { global $master; if (!isset($master)) { print "creating new list $name<Br>\n"; $master = $this; $master->list = array(); } else { print "creating new list $name... using global list<br>\n"; } $this->list =& $master->list; return ($this); } function show() { if (is_array($this->list)) { if (count($this->list)) { reset($this->list); while (list($key, $value) = each($this->list)) { print "list[$key] => $value<br>\n"; } } else { print "list empty.<br>\n"; } } else { print "list is not a list.<br>\n"; } } function append($val) { if (!is_array($this->list)) { print "forced to create list...<br>\n"; $this->list = array(); } else { print "appending $val to list<br>\n"; $this->list[] = $val; } } } $a = new StaticList("a"); $a->append(1); $a->append(2); $b = new StaticList("b"); $b->append(3); $b->append(4); $a->append(5); $b->append(6); print "Contents of a:<br>\n"; $a->show(); print "Contents of a:<br>\n"; $b->show();

« previous php.notes (#162590) next »