douyu53265 2016-01-15 18:24
浏览 44
已采纳

PHP接口编码

I am having a PHP interface coding problem. I took java code from the book “Head First Design Patterns and converted it to the code below. I am using MAMP/ PHP 5.6.2 and NetBeans 8.1.

I am trying to implement an interface “TestInterface” in the Menu class that extends an abstract class (MenuComponent). The Menu class will not start with the “TestInterface” implementation. The code runs when I comment out “TestInterface" in the Menu class declaration as the code below. And while “TestInterface” is commented out, PHP throws no errors even when declaring the interface and keeping the interface function as a Menu member function. I have successfully ran simpler code while extending and implementing at the same time using the same platform as stated above. Because of success with simpler code, I believe there is structural or syntax error in my code below. I hoping that someone can help me find what I am doing wrong. Thanks in advance.

<?php

$run = new myclass;
$run->main();

class myclass {

   private $pancakeHouseMenu;
   private $allMenus;
   private $waitress;

   public function main(){

        echo "<br />hi main!<br />";

        $this->pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast");

        $this->allMenus = new Menu("ALL MENUS", "All menus combind");

        $this->allMenus->add($this->pancakeHouseMenu);

        $this->pancakeHouseMenu->add(new MenuItem(

            "Regular Pancake Breakfast",
            "Pancakes with eggs and sausage"));

        $this->waitress = new Waitress($this->allMenus);

        $this->waitress->printMenu(); 

    }

}

interface TestInterface {

    public function interfaceTest();


}

abstract class MenuComponent {

    public function add(MenuComponent $newMenuComponent) {

            throw new InvalidArgumentException("Exception thrown");

    }

    public function getName() {

            throw new InvalidArgumentException("Exception thrown");

    }

    public function getDescription() {

            throw new InvalidArgumentException("Exception thrown");

    }

    public function printOut() {

            throw new InvalidArgumentException("Exception thrown");

    }

}

class Waitress {

    private $allMenus;

    public function __construct(MenuComponent $allMenus) {

        $this->allMenus = $allMenus;
        $this->allMenus->add($allMenus);

    }

    public function printMenu() {

        $this->allMenus->printOut();

    }

}

class MenuItem extends MenuComponent {

    private $name;
    private $description;

    public function __construct($name, $description) {

        $this->name = $name;
        $this->description = $description;

    }

    public function getName() {

        return $this->name;

    }

    public function getDescription() {

        return $this->description;

    }

    public function printOut() {

        print(" " . $this->getName());   
        print("  -- " . $this->getDescription());

    }

}

class Menu extends MenuComponent /*** implements TestInterface ***/ {

    private $menuComponents = array();
    private $name;
    private $description;
   // private $testVar;

    public function __construct($name, $description) {

       $this->name = $name;
       $this->description = $description;
       $this->testVar = "Interface test succeeded";

    }

     public function interfaceTest(){

        return $this->testVar;

    }

    public function add(MenuComponent $newMenuComponent) {

        array_push($this->menuComponents, $newMenuComponent);

    }

    public function getName() {

       return $this->name;

    }

    public function getDescription() {

        return $this->description;

    }

    public function printOut() {

        print("<br />" . $this->getName());
        print(", " . $this->getDescription());
        print("<br />---------------------");
        print("<br />Testing interface var: ". $this->interfaceTest());

    }

}

?>
  • 写回答

1条回答 默认 最新

  • dongle2627 2016-01-15 20:31
    关注

    In your code you create an object above the declaration of your classes. This seems to be ok if your classes do not implement any interfaces. Since your class menu does implement the interface TestInterface, PHP does not accept your object instantiation before the declaration of your classes.

    The solution is quite simple, place your object creation of myclass below the object declaration:

    <?php
    class myclass {
    
        private $pancakeHouseMenu;
        private $allMenus;
        private $waitress;
    
        ...
    
        public function getDescription() {
    
              return $this->description;
    
         }
    
         public function printOut() {
    
              print("<br />" . $this->getName());
              print(", " . $this->getDescription());
              print("<br />---------------------");
              print("<br />Testing interface var: ". $this->interfaceTest());
    
         }
    
    }
    
    $run = new myclass;
    $run->main();
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮