PHP 4 OOP problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hey all,

I wrote a class to represent a node in a site navigation tree which  
contains methods for building the tree from the database. I also wrote  
some routines to print out the whole tree, starting from the root.

This malfunctions in a weird way in PHP 4 (I get a truncated tree). In  
desperation, I downloaded the trial of PHPEdit to use the debugger, and  
the program worked flawlessly! I suspect that the difference is that  
PHPEdit uses PHP 5 while my testing server and my production server are  
using PHP 4 (Both were operating on the exact same data).

Can anyone spot any obvious problems?

//Display code, starting from node with id 7:

function display_tree($root, $indent)
     for ($i=0; $i<$indent; $i++) {
         echo "&nbsp;";

     echo $root->name."<br>\n";
     for ($i=0; $i<$root->count(); $i++) {

$tree=new NavNode();
while ($tree->level()>0) {


// The tree code:

//the highest node

//represents a node in a navigation tree
class NavNode {

     var $children;
     var $parent, $parentid, $id;
     var $pageid;
     var $name;

     function count()
         return count($this->children);

     function getchild($index)
         return $this->children[$index];

     function level()
         if (isset($this->parent)) {
             return $this->parent->level()+1;
         } else {
             return 0;

     function NavNode()

     function buildup($id, &$childnode)
         //find out who our parents are
         $result=mysql_query("SELECT parent,pageid,name,flags FROM  
structure WHERE id=".$id) or die("Buildup: ".mysql_error());

         if (mysql_num_rows($result)==0) {
             //we don't actually exist


         //build our parent
         if ($this->parentid != 0) {
             $this->parent=new NavNode();
             global $nav_rootnode;

     function builddown(&$childnode)
         //build our children
         $result=mysql_query("SELECT id,pageid,name,flags FROM structure  
WHERE parent=".$this->id) or die("Build down: ".mysql_error());
         while ($row=mysql_fetch_row($result)) {
             //we've already built and been provided with this child
             if (isset($childnode) && ($childnode->id==$row[0])) {
             $child=new NavNode();
             if ($row[3]=="expandall") {

     function buildtree($id,$childnode)


function buildlinkbox($node, $level)
     for ($i=0; $i<$node->count(); $i++) {

         for ($s=0; $s<$level; $s++) {
             echo " ";
         echo "<a  

Thanks so much if anyone can help me out here.

Nicholas Sherlock

Re: PHP 4 OOP problem

Nicholas Sherlock wrote:
Quoted text here. Click to load it

I finally tracked it down to the stupid way that PHP 4 makes a copy of  
an object unless you explicitly pass it by reference with the '&'  
character. I thought that I had it covered, I sprinkled & everywhere it  
would go when I worked with my tree. I eventually tracked the problem  
down to the code I used for adding a child to the list of child nodes:

     function builddown(&$childnode)

This results in a copy of $childnode being pushed on to the array, not a  
reference to $childnode. Array_push doesn't take a reference for the  
object being pushed. Solution:


(There is probably a neater way of doing that)

If you want OOP, don't use PHP 4. I wish I had the choice.

Nicholas Sherlock

Re: PHP 4 OOP problem

Do $this->children[] =& $childnode instead.

Tree structures are pretty tricky to deal with in PHP, even in version
5. I usually try to flatten the structure to a hash table instead. You
can usually get away with that because you usually only present the
tree once.

Site Timeline