Re: note 25622 added to language.oop.newref
| From: | Victor Boivie | Date: | Mon, 30 Sep 2002 21:33:01 +0000 |
| Subject: | Re: note 25622 added to language.oop.newref | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-37443@lists.php.net to get a copy of this message | ||
This is just me trying to resurrect the dead note nr 13489 I accidently
deleted.
// victor@php.net
----- Original Message -----
From: "Rick Php Net" <rick_php_net@rack1.php.net>
Newsgroups: php.notes
To: <php-notes@lists.php.net dot@rack1.php.net>; <net@rack1.php.net>
Sent: Monday, September 30, 2002 11:37 PM
Subject: note 25622 added to language.oop.newref
> 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
>