note 25622 added to language.oop.newref
| From: | rick_php_net at rack1 dot php dot net | Date: | Mon, 30 Sep 2002 21:37:37 +0000 |
| Subject: | note 25622 added to language.oop.newref | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-37441@lists.php.net to get a copy of this message | ||
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();
--
http://www.php.net/manual/en/language.oop.newref.php
http://master.php.net/manage/user-notes.php?action=edit+25622
http://master.php.net/manage/user-notes.php?action=delete+25622
http://master.php.net/manage/user-notes.php?action=reject+25622