Complete the code to declare an interface named Vehicle.
<?php
interface [1] {
public function start();
}
?>The interface name should be Vehicle as declared.
Complete the code to make class Car implement the Vehicle interface.
<?php
interface Vehicle {
public function start();
}
class Car [1] Vehicle {
public function start() {
echo "Car started";
}
}
?>In PHP, classes use the keyword implements to use an interface.
Fix the error in the code by completing the blank with the correct keyword to declare an interface.
<?php
[1] Vehicle {
public function start();
}
?>The keyword to declare an interface in PHP is interface.
Fill both blanks to complete the code that shows why interfaces are needed by enforcing a method in multiple classes.
<?php
interface Logger {
public function [1]();
}
class FileLogger implements Logger {
public function [2]() {
echo "Logging to a file.";
}
}
?>The method log is declared in the interface and implemented in the class to ensure consistent behavior.
Fill all three blanks to complete the code that uses interfaces to enforce multiple classes to have a common method and demonstrate polymorphism.
<?php
interface Shape {
public function [1]();
}
class Circle implements Shape {
public function [2]() {
return "Drawing Circle";
}
}
class Square implements Shape {
public function [3]() {
return "Drawing Square";
}
}
?>The method draw is declared in the interface and implemented by both classes to ensure they have the same method for drawing shapes.