Reputation: 47
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());
}
}
?>
Upvotes: 0
Views: 99
Reputation: 46
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();
?>
Upvotes: 3