0

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
  • Please add the Menu class as that (and the interface) is what's relevant) Commented Jan 15, 2016 at 18:26

1 Answer 1

3

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

?>
Sign up to request clarification or add additional context in comments.

2 Comments

Hey Steven, thank you so much. Is this issue the result of php being an interpreted language? And if I'm not mistaking Java is a compiled language, therefore I probably wouldn't have this structural problem with Java; would I be correct?
Now that I'm running the program as you showed me above I realize that my question regarding php being an interpreted lang. might be a stupid question. But please comment anyhow because I don't like not asking in fear of stupidity. And I appreciate your expertise. Thanks again Steven.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.