duanqianpu6499 2014-02-27 14:53
浏览 48
已采纳

对象数组出错

I am new in PHP and in Design Patterns. I have been studying the book Head First - Design patterns with codes in Java. This is the PHP version of one of its Composite Patterns. However, I am getting strange errors with the array $menuComponents presented below inside class Menu.

<?php

abstract class MenuComponent{
    // Composite Methods
    function add(MenuComponent $menuComponent){
        throw new RuntimeException("Erro!");
    }
    function remove(MenuComponent $menuComponent){
        throw new RuntimeException("Erro!");
    }
    function getChild($i){
        throw new RuntimeException("Erro!");
    }
    // Operation Methods
    function getName(){
        throw new RuntimeException("Erro!");
    }
    function getDescription(){
        throw new RuntimeException("Erro!");
    }
    function getPrice(){
        throw new RuntimeException("Erro!");
    }
    function isVegetarian(){
        throw new RuntimeException("Erro!");
    }
    function printMethod(){
        throw new RuntimeException("Erro!");
    }
}

class MenuItem extends MenuComponent{
    private $name;
    private $description;
    private $vegetarian;
    private $price;

    public function __construct($name,$description,$vegetarian,$price){
        $this->name=$name;
        $this->description=$description;
        $this->vegetarian=$vegetarian;
        $this->price=$price;
    }
    public function getName(){
        return $this->name;
    }
    public function getDescription(){
        return $this->description;
    }
    public function getPrice(){
        return $this->price;
    }
    public function isVegetarian(){
        return $this->isVegetarian;
    }
    public function printMethod(){
        echo ' '.$this->name.' ';
        if($this->isVegetarian){
            echo 'v ';
        }
        echo $this->price;
        echo '<br>';
    }
}

class Menu extends MenuComponent{
    private $name;
    private $description;
    private $menuComponents=array();

    public function __construct($name,$description) {
    $this->name = $name;
    $this->description = $description;
    }

    public function add(MenuComponent $menuComponent) {
        $this->menuComponents=array_push($this->menuComponents,$menuComponent);
    }

    public function remove(MenuComponent $menuComponent) {
        $this->menuComponents=array_diff($this->menuComponents,$menuComponent);
    }

    public function getChild($i) {
            return $this->menuComponents[$i];
    }

    public function getName() {
            return $this->name;
    }

    public function getDescription() {
            return $this->description;
    }
    public function printMethod(){
        echo ' '.$this->getName().", ";
        echo ' '.$this->getDescription()."<br>";
        echo "---------------------- <br>";
        foreach($this->menuComponents as $components){
            echo $components->printMethod()."<br>";
        }

    }
}


class Waitress{
    private $allMenus;
    public function __construct(MenuComponent $allMenus){
        $this->allMenus=$allMenus;
    }
    public function printMenu(){
        $this->allMenus->printMethod();
    }
}

$pancakeHouseMenu=new Menu('PANCAKE HOUSE MENU','Breakfast');
$dinnerMenu=new Menu('DINNER MENU','Lunch');
$cafeMenu=new Menu('CAFE MENU','Dinner');
$dessertMenu=new Menu('DESSERT MENU','Desert of course');

$allMenus=new Menu('All Menus','All menus combined');

$allMenus->add($pancakeHouseMenu);
print_r($allMenus);
$allMenus->add($dinnerMenu);
$allMenus->add($cafeMenu);

$dinnerMenu->add(new MenuItem('Past', 'Spagueti with sauce', true, 3.89));
$dinnerMenu->printMethod();

The output of print_r is

Menu Object ( [name:Menu:private] => All Menus [description:Menu:private] => All menus combined [menuComponents:Menu:private] => 1 ) 

Note that there is a strange "1" inside menuComponents, where it should be $dinnerMenu

After this print_r I get the following errors:

Warning: array_push() expects parameter 1 to be array, integer given in /var/www/DesignPatternHeadFirst/compositePattern.php on line 80

Warning: array_push() expects parameter 1 to be array, null given in /var/www/DesignPatternHeadFirst/compositePattern.php on line 80

And then the output of $dinnerMenu->printMethod() is

DINNER MENU, Lunch
---------------------- 

and then another error

Warning: Invalid argument supplied for foreach() in /var/www/DesignPatternHeadFirst/compositePattern.php on line 102

I must be doing something very wrong! What is it?

I appreciate any help!

  • 写回答

1条回答 默认 最新

  • douchuang4402 2014-02-27 14:56
    关注

    You are doing:

    public function add(MenuComponent $menuComponent) {
        $this->menuComponents=array_push($this->menuComponents,$menuComponent);
    }
    

    You have to remember, that array_push returns integer, so you override this array type field with integer value.

    Try with:

    public function add(MenuComponent $menuComponent) {
        array_push($this->menuComponents,$menuComponent);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题