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();
}
}
}
?>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>";
?>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