Question

So I decided to make some classes in PHP so I can dynamically construct a menu bar using <ul>, <li> and <a> tags. Reason why is because I want to reuse this across projects.

First the Blob of Code, notice that I put in some debugging info in my code, because I'm actually a .NET developer and I dunno how to decently debug yet :s

<?php

// Menu Items <li><a href=\"$target\">$itemName</a></li>
class MenuItem {
    public $order;
    public $target;
    public $itemName;

function __construct($itemName="", $target="", $order=0) {
    $this->order = $order;
    $this->target = $target;
    $this->itemName = $itemName;
}

public function Build() {
    //if $target is empty or $itemName is empty then return empty string (both must be present to generate an item)
    if(strcasecmp($target,"") == 0 || strcasecmp($itemName,"") == 0) { 
        return "";
    }
    return "<li><a href=\"" . $target . "\">" . $itemName . "</a></li>";
}
}

//<li><a>$FirstMenuItem</i>
// Submenu Root Head Item: <li><a href=\"$(optional)Target\">$ItemName</a><ul>
// Holds a list of MenuItems and SubmenuRootItems
// Submenu Root End Item: </ul>
class SubMenu extends MenuItem {
    protected $itemArray;

    function __construct($itemName = "", $target = "", $order = 0) {
        parent::__construct($itemName, $target, $order);
        $this->itemArray = array();
    }

    public function Items() {
        return $this->itemArray;
    }

    public function AddSubMenuItem ($subMenuItem) {
        $retval = strcasecmp( get_class ($subMenuItem), "submenu") == 0 ? self::AddItem($subMenuItem) : false;
       echo"<br>second dump<br>";
        var_dump($this->itemArray); echo "<br>";
        return $retval;
    }

    public function AddMenuItem ($menuItem) {
        $retval =  strcasecmp( get_class ($menuItem), "menuitem") == 0 ? self::AddItem($menuItem) : false;
        echo"<br>second dump<br>"; //debug code
        var_dump($this->itemArray); echo "<br>"; //debug code
        return $retval;
    }

    protected function AddItem($item) {
        $itemArray[$item->itemName] = $item;
        echo "AddItem <br />"; //ugly debug code
        var_dump($itemArray); //even more of it
        return true;
    }

    public function Build() {
        $buildHtml = "<li>";
        $buildHtml .= strcasecmp($target, "") == 0 ?  "<a>" : "<a href=\"" . $target . "\">";
        $buildHtml .= $itemName . "</a><ul>";
        $buildHtml .= BuildSubMenu(Items());
        $buildHtml .= "</ul></li>";
    }

    public function BuildSubMenu($menuItems) {
        $buildHtml = "";
        echo "for each start". var_dump($menuItems) ;
        foreach($menuItems as $menuItem) {
            echo $menuItem;
            $buildHtml .= $menuItem->Build();
        }

        return $buildHtml;
    }
}

// Menu Root Element <ul id=\"menu\">
// holds a list of MenuItems and SubmenuRootItems
// Menu Root Closing Element </ul>
class MenuBuilder extends SubMenu {
    function __construct() {
        parent::__construct(); 
    }

    public function Build() {  echo "<br><br><br>". var_dump($this->itemArray); echo "<br><br><br>".var_dump(parent::$this->itemArray);echo "<br><br><br>";//ugly debug code

        $buildHtml = "<ul id=\"menu\">";
        $buildHtml .= parent::BuildSubMenu($this->itemArray);
        $buildHtml .= "</ul>";

        return $buildHtml;
    }
}
?>

The problem is that the whole thing isn't working, php obviously isn't .NET, but I really don't know how the following can be true. To test I made this little php page that does some calls to my new classes.

Write("Create menuBuilder...");
$menuBuilder = new MenuBuilder();
WriteLine("Done!");

Write("Create subMenu1...");
$subMenu1 = new SubMenu("Sub Menu 1");
WriteLine("Done!");

Write("Execute subMenu1->AddMenuItem...");
$subMenu1->AddMenuItem(new MenuItem("Menu Item 1.1", "#1.1"));
WriteLine("Done!");

Write("Create menuItem2...");
$menuItem2 = new MenuItem("Menu Item 2","#2");
WriteLine("Done!");

Write("Execute menuBuilder->AddSubMenuItem...");
$menuBuilder->AddSubMenuItem($subMenu1);
WriteLine("Done!");
Write("Execute menuBuilder->AddMenuItem...");
$menuBuilder->AddMenuItem($menuItem2);
WriteLine("Done!");

and after that there is a dump of the Items property on both $subMenu and $menuBuilder

The output in my browser of these calls baffles me completely:

Create menuBuilder...Done!
Create subMenu1...Done!
Execute subMenu1->AddMenuItem...AddItem 
array(1) { ["Menu Item 1.1"]=> object(MenuItem)#4 (3) { ["order"]=> int(0) ["target"]=>    string(4) "#1.1" ["itemName"]=> string(13) "Menu Item 1.1" } } 
second dump
array(0) { } 
Done!
Create menuItem2...Done!
Execute menuBuilder->AddSubMenuItem...AddItem 
array(1) { ["Sub Menu 1"]=> object(SubMenu)#3 (4) { ["itemArray":protected]=> array(0) { } ["order"]=> int(0) ["target"]=> string(0) "" ["itemName"]=> string(10) "Sub Menu 1" } } 
second dump
array(0) { } 
Done!
Execute menuBuilder->AddMenuItem...AddItem 
array(1) { ["Menu Item 2"]=> object(MenuItem)#4 (3) { ["order"]=> int(0) ["target"]=> string(2) "#2" ["itemName"]=> string(11) "Menu Item 2" } } 
second dump
array(0) { } 
Done!

So the AddItem function works, an item is added to the $itemArray and it gets dumped successfully. Then when the function returns to it's caller 'AddMenuItem' and I dump the same $itemArray again it's empty? I really don't get how this works :s

Was it helpful?

Solution

Maybe you won't change the class itemArray property in the AddItem method?

You have this:

protected function AddItem($item) {
    $itemArray[$item->itemName] = $item;
    echo "AddItem <br />"; //ugly debug code
    var_dump($itemArray); //even more of it
    return true;
}

Try this one:

protected function AddItem($item) {
    // $this will change the class property :)
    $this->itemArray[$item->itemName] = $item;
    echo "AddItem <br />"; //ugly debug code
    var_dump($this->itemArray); //even more of it
    return true;
}

You won't use $this in many places like in the MenuItem->Build method:

class MenuItem {
    public $order;
    public $target;
    public $itemName;

function __construct($itemName="", $target="", $order=0) {
    $this->order = $order;
    $this->target = $target;
    $this->itemName = $itemName;
}

public function Build() {
    // The $target and $itemName variables aren't inicialized
    //if $this->target is empty or $this->itemName is empty then return empty string (both must be present to generate an item)
    if(strcasecmp($this->target,"") == 0 || strcasecmp($this->itemName,"") == 0) { 
        return "";
    }
    return "<li><a href=\"" . $this->target . "\">" . $this->itemName . "</a></li>";
}
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top