|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[1999-07-11 06:41 UTC] sas at cvs dot php dot net
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 08 20:00:02 2026 UTC |
PHP confuses something when I have a hierarchy of objects (an object having an array of objects which themselves have arrays of objects ...) and trying to walk down the structure. In the method invocation of the second level, the array holding the children pretends to be empty when it's not. Invoking the method of the object in question (in the second level) directly reveals that the array is not really empty; it works, although *only to the second level* again. I suspect that there is some static variable in the code which handles method calls. In the following code, the array of the "html" object seems to be empty in the printChildren method when it's called within the hierarchy. Calling the printOut method of this object directly (in the last line) lets it work again, but now the same error occurs in the "body" object one level lower. Strange enough, when I change the code to add the hierarchy bottom-up instead of top-down, then it works! <?php class _Base { var $attributes = array (); function _Base () { print "<pre>New base class</pre>\n"; } function set ($name, $value = "") { $attributes[$name] = $value; } function addTo ($container) { $container->addChild ($this); } } class _Container extends _Base { var $children = array (); function _Container () { print "<pre>New container</pre>\n"; } function add ($child) { $this->children[] = $child; //print "<pre>Added child to $this->tag: $child->tag. New count = ".count ($this->children)."</pre>\n"; } function printAttr () { reset ($this->attributes); while ($a = each ($this->attributes)) { print " $a[0]".!empty ($a[1])?"=\"$a[1]\"":""; } } function printChildren () { print "<pre>".count($this->children)." children ($this->children) of $this->tag:</pre>\n"; reset ($this->children); while (list ($k, $v) = each ($this->children)) { print "<pre>Printing child $k: $v->tag</pre>\n"; $v->printOut (); } } function printOut () { print "<pre>Printing $this->tag</pre>"; if (!empty ($this->tag)) { print "<$this->tag"; $this->printAttr (); print ">\n"; } $this->printChildren (); print "</$this->tag>\n"; print "<pre>Done printing $this->tag</pre>"; } } class Document extends _Container { function Document () { print "<pre>New document</pre>\n"; } } class Text extends _Base { var $tag; function Text ($text) { $this->tag = $text; } function printOut () { print $this->tag; } } class Html extends _Container { var $tag = "html"; function Html () { print "<pre>New Html</pre>\n"; } } class Body extends _Container { var $tag = "body"; function Body () { print "<pre>New Body</pre>\n"; } } $doc = new Document; $html = new Html; $doc->add ($html); $body = new Body; $html->add ($body); $t1 = new Text ("This is my text. "); $body->add ($t1); $t2 = new Text ("This is another text. "); $body->add ($t2); $doc->printOut (); $html->printOut (); ?>