Page 1 of 1

[SOLVED] OO: tree programming

Posted: Thu Dec 11, 2003 2:19 am
by aquila125
Hi there..

I'm building a tree-class for a menu generating script..

this is my code:

Code: Select all

<?php
class MenuItem{

	
	var $buildDirection = array("hor","vert","vert","vert");
							//in what direction should the menu on $this depth be build?	
	var $menuitems=array(); 	//holds children
	var $top=0;
	var $left=0;
	var $width=150;			//width of table
	var $height=50; 			//height of center cell
	var $link="";			//link when clicked
	var $parent="";			//parent of the class, empty if root
	var $depth=0;			//depth of tree, 0 is root
	var	$name;
	
	
	function MenuItem($cMenuItem,$iDepth,$sName){
		
		$this->parent=$cMenuItem;
		$this->depth=$iDepth;
		$this->name=$sName;
		echo "Create:".$this->name."<br>";
		echo "Parent:".$this->parent."<br>";
	}
	
	function AddItem($mi){
		$this->menuitems[]=$mi;
		echo "Current item: ".$this->name." Size: ".count($this->menuitems)."<br>";
	}
	
	function BuildMenu(){
		echo "Current item: ".$this->name." Size: ".count($this->menuitems)."<br>";		
		foreach ($this->menuitems as $mi){
			echo "Building: ".$mi->name."<br>";
			$mi->BuildMenu();
			
			
		}
		
	}
}
?>
To instantiate the class I use this code:

Code: Select all

<?php
<?
include "MenuItemClass.php";

$root= new MenuItem("",0,"root");
$root1=new MenuItem("root",1,"root1");
$root->AddItem($root1);
$root1->AddItem(new MenuItem("root1",2,"root2"));
$root1->AddItem(new MenuItem("root1",2,"root3"));
echo "SIZE ROOT1: ".count($root1->menuitems)."<br>";
$root->BuildMenu();
echo "SIZE ROOT1: ".count($root1->menuitems)."<br>";

?>
If I add stuff to root1, the size of the array menuitems (from root1) grows (as it should be).. but if I recursively call BuildMenu it says that the array is empty? (although SIZE ROOT1 in my index.php still gives size = 2 (before and after the BuildMenu call)

What am I doing wrong?


This is the output I get:

Code: Select all

Create:root
Parent:
Create:root1
Parent:root
Current item: root Size: 1
Create:root2
Parent:root1
Current item: root1 Size: 1
Create:root3
Parent:root1
Current item: root1 Size: 2
SIZE ROOT1: 2
Current item: root Size: 1
Building: root1
Current item: root1 Size: 0
SIZE ROOT1: 2

Posted: Thu Dec 11, 2003 2:25 am
by aquila125
I found the answer..
I should'v added root2 and root3 to root1 BEFORE adding root1 to root...

or added an & to make sure I used pointers instead of references...

Posted: Thu Dec 11, 2003 10:28 am
by dev2761
care to elaborate please?

Posted: Thu Dec 11, 2003 12:16 pm
by aquila125
Sure:

I used references instead of pointers. So when I added root1 to root, php made a copy and stored this copy of root1 inside root.
Adding stuff to root1 did not have any effect on the copy available to root.

By changing this to pointers php doesn't create a copy of the object, but uses the same object. So any changes to root1 will be noticable in the object that's known by root

Posted: Thu Dec 11, 2003 12:39 pm
by Weirdan
aquila, I understand you, but it's better to use common terminology.
There are no pointers in PHP. when you pass something to function using & syntax it's usually called `pass by reference`. References in PHP are something like C pointers, actually they have more in common with C++ references.

Posted: Thu Dec 11, 2003 1:15 pm
by aquila125
sorry.. I'm used to the java terminology.. I'll make sure to get it right next time..