Classes and objects provide named types with methods and properties that can be inspected or organized.
What Is Interfaces and Abstract Classes in PHP?
Interfaces define contracts, inheritance specializes related types, and composition delegates responsibilities to collaborators.
Example 1: Understanding the Basic Behavior
<?php
interface Reporter { public function report(): string; }
class JsonReporter implements Reporter { public function report(): string { return '{"ok":true}'; } }
echo (new JsonReporter())->report();
Output:
{"ok":true}
Explanation: Interfaces define contracts, inheritance specializes related types, and composition delegates responsibilities to collaborators.
Example 2: Testing Another Case
<?php
class Logger { public function write(string $message): void { echo $message; } }
class Worker { public function __construct(private Logger $logger) {} public function run(): void { $this->logger->write('working'); } }
(new Worker(new Logger()))->run();
Output:
working
Explanation: Prefer a small interface and composition when different implementations do not share a genuine is-a relationship.
Practical Example: Applying the Operation
<?php
abstract class Payment { abstract public function label(): string; }
class CardPayment extends Payment { public function label(): string { return 'Card'; } }
echo (new CardPayment())->label();
Output:
Card
Explanation: Visibility and inheritance affect which members exist and can be accessed directly.
Important Behavior and Edge Cases
- Prefer a small interface and composition when different implementations do not share a genuine is-a relationship.
- Visibility and inheritance affect which members exist and can be accessed directly.
Comparison With Related Approaches
| Approach | When to Use It |
|---|---|
Interfaces and Abstract Classes | Interfaces define contracts, inheritance specializes related types, and composition delegates responsibilities to collaborators. |
Abstract classes | An interface declares a contract that multiple unrelated classes can implement; an abstract class can share implemented methods and protected state. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Misinterpreting the documented behavior of Interfaces and Abstract Classes() for the supplied value. | Prefer a small interface and composition when different implementations do not share a genuine is-a relationship. |
| Choosing Abstract classes without checking whether it provides the same result. | An interface declares a contract that multiple unrelated classes can implement; an abstract class can share implemented methods and protected state. |
Key Takeaways
- Interfaces define contracts, inheritance specializes related types, and composition delegates responsibilities to collaborators.
- Prefer a small interface and composition when different implementations do not share a genuine is-a relationship.
- Visibility and inheritance affect which members exist and can be accessed directly.
The examples demonstrate where Interfaces and Abstract Classes fits within object-oriented php. Verify the relevant preconditions whenever you adapt the code to real data.