我遇到了PHP接口编码问题。我从“Head First Design Patterns”一书中获取了Java代码并将其转换为下面的代码。我正在使用MAMP / PHP 5.6.2和NetBeans 8.1。
我正在尝试在Menu类中实现扩展抽象类(MenuComponent)的接口“TestInterface”。 Menu类不会以“TestInterface”实现开头。当我注释掉“TestInterface"在Menu类声明中作为下面的代码。虽然“TestInterface”已被注释掉,但即使在声明接口并将接口函数保持为Menu成员函数时,PHP也不会抛出任何错误。我已成功运行更简单的代码,同时使用与上述相同的平台同时扩展和实现。由于代码更简单,我相信我的代码中存在结构或语法错误。我希望有人可以帮我找到我做错的事。提前谢谢。
<?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());
}
}
?>
答案 0 :(得分:3)
在您的代码中,您可以在类的声明之上创建一个对象。如果您的类不实现任何接口,这似乎没问题。由于您的类menu
确实实现了接口TestInterface
,因此PHP在声明类之前不接受您的对象实例化。
解决方案非常简单,将对象创建myclass
置于对象声明之下:
<?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();
?>