problem storing objects in arrays

Discussion in 'PHP' started by maikelvanmaurik, May 17, 2006.

  1. #1
    hi i was working on a tree class which worked fine when running it locally using php 5.1.2, but when uploaded to my server it didn't work anymore. The server uses php 4.3.10. can anyone please help the problem seems to be that i cannot store object's in the collection array running php 4.3.10.

    class Mainframe_Gui_Tree {
    
    	var $nodes;
    	
    	function Mainframe_Gui_Tree() {
    		$this->nodes = new Mainframe_Gui_Tree_NodeCollection();
    	}
    	
    	function output() {
    		echo "<pre>";
    		print_r( $this->nodes );
    		echo "</pre>";
    	}
    }
    
    class Mainframe_Gui_Tree_NodeCollection {
    
    	var $collection;
    	
    	function Mainframe_Gui_Tree_NodeCollection() {
    		$this->collection = array();
    	}
    	
    	function addChild( $parent, $name="", $link="", $title="", $target="", $iconCollapsed="", $iconExpanded="" ) {
    		$newNode = new Mainframe_Gui_Tree_Node( $parent, $name="", $link="", $title="", $target="", $iconCollapsed, $iconExpanded );
    		if ( is_object( $parent ) ) {
    			array_push( $parent->_childeren, $newNode );
    		} else {
    			array_push( $this->collection, $newNode );
    		}
    		return $newNode;
    	}
    
    }
    
    class Mainframe_Gui_Tree_Node {
    	
    	var $_childeren;
    	var $_level;
    	
    	function Mainframe_Gui_Tree_Node( $parent, $name="", $link="", $title="", $target="", $iconCollapsed="", $iconExpanded="" ) {
    		$this->_childeren = array();
    	}
    	
    	function setLevel( $level ) {
    		$this->_level = $level;
    	}
    	
    }
    Code (markup):
    example:

    $tree = new Mainframe_Gui_Tree();
    	
    	$rootNode = $tree->nodes->addChild( NULL, "test" );
    	$tree->nodes->addChild( $rootNode, "bla" );
    
    	
    	$tree->output();
    Code (markup):
    when I run the example locally I get the following:

    Mainframe_Gui_Tree_NodeCollection Object
    (
        [collection] => Array
            (
                [0] => Mainframe_Gui_Tree_Node Object
                    (
                        [_childeren] => Array
                            (
                                [0] => Mainframe_Gui_Tree_Node Object
                                    (
                                        [_childeren] => Array
                                            (
                                            )
    
                                        [_level] => 
                                    )
    
                            )
    
                        [_level] => 
                    )
    
            )
    
    )
    Code (markup):
    which is good because the nodes are nested, but when running it on the server I get:

    mainframe_gui_tree_nodecollection Object
    (
        [collection] => Array
            (
                [0] => mainframe_gui_tree_node Object
                    (
                        [_childeren] => Array
                            (
                            )
    
                        [_level] => 
                    )
    
            )
    
    )
    Code (markup):
    There a no childeren added here. what I also have noticed is that the class name is lowercase is this normal. thanks
     
    maikelvanmaurik, May 17, 2006 IP
  2. tolra

    tolra Active Member

    Messages:
    515
    Likes Received:
    36
    Best Answers:
    1
    Trophy Points:
    80
    #2
    The problem is cause by the fact that PHP5 passes objects by reference while PHP4 doesn't unless you explicitly ask it to.

    Below I have pasted the modifications to get your test example to work, however there might be other places you need to force by reference.
    	function & addChild(& $parent, $name="", $link="", $title="", $target="", $iconCollapsed="", $iconExpanded="" ) {
    		$newNode = new Mainframe_Gui_Tree_Node($parent, $name="", $link="", $title="", $target="", $iconCollapsed, $iconExpanded );
    		if ( is_object( $parent ) ) {
    			$parent->_childeren[] =& $newNode;
    		} else {
    			$this->collection[] =& $newNode;
    		}
    		return $newNode;
    	}
    
    Code (markup):
    $tree = new Mainframe_Gui_Tree();
    	
    $rootNode =& $tree->nodes->addChild($o = NULL, "test" );
    $tree->nodes->addChild( $rootNode, "bla" );
    
    	
    $tree->output();
    Code (markup):
     
    tolra, May 18, 2006 IP