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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法