Interfaces help make sure different parts of a program can work together by agreeing on what methods they must have.
Why interfaces are needed in PHP
<?php interface InterfaceName { public function method1(); public function method2($param); } class ClassName implements InterfaceName { public function method1() { // code here } public function method2($param) { // code here } } ?>
Interfaces only declare methods, they do not provide the method's code.
Classes that implement an interface must write the code for all its methods.
Logger with one method. The class FileLogger implements it by writing the message.<?php interface Logger { public function log(string $message); } class FileLogger implements Logger { public function log(string $message) { echo "Logging to a file: $message"; } } ?>
Shape interface requires an area method. The Circle class implements it with its own calculation.<?php interface Shape { public function area(); } class Circle implements Shape { private float $radius; public function __construct(float $radius) { $this->radius = $radius; } public function area() { return pi() * $this->radius * $this->radius; } } ?>
This program shows an interface Vehicle with a method startEngine. Two classes implement it differently. A function uses the interface type to start engines without caring about the exact vehicle type.
<?php interface Vehicle { public function startEngine(); } class Car implements Vehicle { public function startEngine() { echo "Car engine started."; } } class Motorcycle implements Vehicle { public function startEngine() { echo "Motorcycle engine started."; } } function startVehicleEngine(Vehicle $vehicle) { $vehicle->startEngine(); } $car = new Car(); $motorcycle = new Motorcycle(); startVehicleEngine($car); echo "\n"; startVehicleEngine($motorcycle); ?>
Interfaces help keep code organized and clear by defining what methods a class must have.
They allow different classes to be used interchangeably if they implement the same interface.
Interfaces define a set of methods without code.
Classes that implement interfaces must write all those methods.
This helps different classes work together and makes code easier to manage.
Practice
Solution
Step 1: Understand the purpose of interfaces
Interfaces only declare method names without code, setting a contract for classes.Step 2: Recognize what interfaces enforce
Classes implementing interfaces must write all declared methods, ensuring consistent behavior.Final Answer:
To define a set of methods that classes must implement -> Option DQuick Check:
Interfaces = method contracts [OK]
- Thinking interfaces store data
- Believing interfaces create objects
- Assuming interfaces run code automatically
Solution
Step 1: Recall PHP interface syntax
Interfaces use the keyword 'interface' followed by the name and method declarations without bodies.Step 2: Check method declaration rules
Methods in interfaces must be declared without code (no braces), only the signature.Final Answer:
interface MyInterface { public function doSomething(); } -> Option BQuick Check:
Interface methods have no body [OK]
- Adding method bodies inside interfaces
- Using parentheses after interface name
- Declaring interfaces as classes
interface Talker {
public function talk();
}
class Person implements Talker {
public function talk() {
return "Hello!";
}
}
$person = new Person();
echo $person->talk();Solution
Step 1: Check interface and class implementation
The class Person implements Talker and defines the talk() method returning "Hello!".Step 2: Understand the output of echo
Calling $person->talk() returns "Hello!", which echo prints.Final Answer:
Hello! -> Option AQuick Check:
Implemented method returns string [OK]
- Expecting error if method is implemented
- Thinking interface code runs directly
- Missing method implementation causes error
interface Flyer {
public function fly();
}
class Bird implements Flyer {
// forgot to implement fly method
}Solution
Step 1: Check interface requirements
Flyer interface requires a fly() method to be implemented by any class using it.Step 2: Verify class Bird implementation
Bird class implements Flyer but does not define fly(), causing a fatal error.Final Answer:
Class Bird must implement method fly() -> Option AQuick Check:
Interface methods must be implemented [OK]
- Assuming interface methods are optional
- Thinking empty class is allowed
- Confusing interface with abstract class
startEngine() method, but their implementations differ. How does using an interface help?Solution
Step 1: Understand interface role in method enforcement
An interface declares startEngine() method that classes must implement, but each class writes its own code.Step 2: Recognize benefit for interchangeable use
Because both classes have startEngine(), code using them can call this method without knowing class details.Final Answer:
It forces both classes to have a startEngine() method, ensuring they can be used interchangeably -> Option CQuick Check:
Interface = method contract, different code [OK]
- Thinking interface shares method code
- Believing methods become optional
- Confusing interface with inheritance
Ready to prove it?
Pass the test and this pattern moves unknown
