对象数组出错

时间:2014-02-27 14:53:34

标签: php arrays

我是PHP和设计模式的新手。我一直在研究Head First - 用Java编写代码模式。这是其复合模式之一的PHP版本。但是,我在类$menuComponents内的数组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();

print_r的输出是

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

请注意,有一个奇怪的&#34; 1&#34;在menuComponents中,它应该是$ dinnerMenu

在此print_r之后,我收到以下错误:

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

然后$dinnerMenu->printMethod()的输出是

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

然后是另一个错误

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

我一定是做错了!它是什么?

我感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

你在做:

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

你必须记住,array_push返回整数,所以你用整数值覆盖这个数组类型字段。

尝试:

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