RE: [PHP] Reference problem
| From: | Martin Towell | Date: | Thu, 28 Feb 2002 22:28:07 +0000 |
| Subject: | RE: [PHP] Reference problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-86668@lists.php.net to get a copy of this message | ||
I wrote an xml parsing class, and xml tag class, that might help you there
see the attachment
-----Original Message-----
From: John English [mailto:je@brighton.ac.uk]
Sent: Friday, March 01, 2002 12:35 AM
To: php-lang@lists.php.net; php-general@lists.php.net
Subject: [PHP] Reference problem
I'm trying to build a tree of objects using PHP4, but I'm getting
tangled in reference problems. Some test code that illustrates the
problem:
class X {
var $parent;
var $ident;
var $kids;
function X (&$parent,$number) {
$this->parent = $parent;
$this->ident = $number;
$this->kids = array();
}
}
$root = new X($here,1);
$here =& $root;
for ($i = 2; $i < 5; $i++) {
$elem = new X($here,$i);
$here->kids[] = $elem;
$here =& $elem; #--- ???
}
print_r($root);
I'm hoping for a structure where each node has a link to the parent
node and an array of child nodes. Instead I get a recursive set of
trees; removing the & on the line marked "???" means I get an empty
kids array in the first child node. I've been sprinkling &'s around
(and unsprinkling them ;-) and my head is starting to hurt.
Can anyone tell me what I'm doing wrong here?
TIA,
-----------------------------------------------------------------
John English | mailto:je@brighton.ac.uk
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
Dept. of Computing | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php