{__STYLE__}
咨询电话:18678002022

php接口编码

2023-02-27 10:48:01 点击:1009次

我有一个PHP接口编码问题。我从“Head First Design Patterns”一书中读取了Java代码,并将其转换为下面的代码。我正在使用MAMP/PHP 5.6.2和NetBeans 8.1。PHP接口编码

我想在扩展抽象类(MenuComponent)的Menu类中实现接口“TestInterface”。 Menu类不会以“TestInterface”实现开始。代码运行时,我将Menu类声明中的“TestInterface”注释为下面的代码;而当“TestInterface”被注释掉时,PHP甚至在声明接口并将接口函数保持为Menu成员函数时也不会引发错误。已经成功运行简单的代码,同时使用上述同一平台进行扩展和实现。

<?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()); 

    } 

} 

?>


上一篇:如何用PHP跳转页面

下一篇:HTML基础标签

返回顶部
返回顶部