Manas Sahoo
Manas Sahoo

Reputation: 91

500 internal error in php while implements an interface

I am facing an issue with this code. This is showing 500 internal error. File name is Dog.php. can someone please help me.

interface Animal{

     public function bark(){ 

     }
     public function eat(){ 

     }

}

class Dog implements Animal {


    public function bark(){

        echo "bark bark";
    }
     public function eat(){
        echo "Biscuits";
    }


}
$d =  new Dog();
$d->bark();

Upvotes: 0

Views: 170

Answers (3)

user8586625
user8586625

Reputation:

An interface is a description of the actions that an object can do. It should only define those actions as function signatures that should not contain any implementation code. The purpose of an interface is to enforce an object to implement those actions. So that's why in your Animal interface you should just define bark and eat function signatures and remove the implementation part (we use {} to start our implementation) and your Dog class should have the implementation of how does the dog will bark and eat.

interface Animal {
     public function bark(); 
     public function eat();
}

Upvotes: 0

l4sh
l4sh

Reputation: 321

Your interface functions should not have a body. You have to declare them like this:

interface Animal{
    public function bark();
    public function eat();
}

For more information check the PHP documentation on interfaces

Upvotes: 1

B. Desai
B. Desai

Reputation: 16436

When you provide interface, Body is not allowed in it

interface Animal{

     public function bark(); //remove body
     public function eat(); //remove body

}

class Dog implements Animal {


    public function bark(){

        echo "bark bark";
    }
     public function eat(){
        echo "Biscuits";
    }


}
$d =  new Dog();
$d->bark();

Upvotes: 1

Related Questions