dpgua04022 2013-12-11 20:02
浏览 284
已采纳

带有受保护数组属性的php继承让我头痛不已

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

  • 写回答

1条回答 默认 最新

  • doudeng2016 2013-12-11 20:09
    关注

    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>";
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?