Interfaces advanced
An interface in PHP is a promise about behaviour, and this lesson treats it as a design tool: keep the promises small, join them when a class needs the whole set, share the constants that belong to the contract, and let a framework interface like Countable reach the built-in functions.
Segregate
Small focused interfaces beat one giant list. Each interface covers a single skill that a class may or may not need.
Classes then use only the parts they need. Tests stay simple because each promise is easy to fake.
<?php interface Readable { public function read(): string; } interface Writable { public function write(string $text): void; } class Reader implements Readable { public function read(): string { return "hi"; } } $r = new Reader(); echo $r->read(), PHP_EOL; echo $r instanceof Writable ? "writable" : "read-only", PHP_EOL;
php segregate.php
hi
read-only
The class promised reading only, so the write promise is not part of it and the check says so.
Extend interfaces
Interfaces extend other interfaces with the same extends keyword classes use. The child interface bundles every parent promise into one name.
A class that uses the child name must fill in all parents too. One name then stands for the full set.
<?php interface Readable { public function read(): string; } interface Writable { public function write(string $text): void; } interface Store extends Readable, Writable { } class Memory implements Store { public function read(): string { return "hi"; } public function write(string $text): void { echo "stored: ", $text, PHP_EOL; } } $m = new Memory(); echo $m->read(), PHP_EOL; $m->write("a"); echo $m instanceof Readable ? "readable" : "not", PHP_EOL; echo $m instanceof Writable ? "writable" : "not", PHP_EOL;
php extend.php
hi
stored: a
readable
writable
The class named one interface and answered for both parents, which is what "one name then stands for the full set" means in practice.
Interface constants
Interfaces may hold constants that every user shares. Status codes and default limits are typical guests there.
Read them through the interface name or through the class name. Both spellings reach the same value.
<?php interface Http { const OK = 200; } class Api implements Http { } echo Http::OK, PHP_EOL; echo Api::OK, PHP_EOL; echo Http::OK === Api::OK ? "same" : "different", PHP_EOL;
php constants.php
200
200
same
The class inherited the constant of the interface, and the strict comparison proves both spellings reach one value.
Countable showcase
The Countable interface lets count work on your object. It asks for a single count method in return.
Return the item total from that method. Client code then calls count on the object like on a plain array.
<?php class Bag implements Countable { public array $items = []; public function count(): int { return count($this->items); } } $bag = new Bag(); $bag->items = ["a", "b", "c"]; echo count($bag), PHP_EOL;
php countable.php
3
The language function reached the method of the object, so no special helper is needed on the calling side.
Exercises
- Split a three-method interface into three small ones and implement them in two classes.
- Build an interface that extends two others and a class that satisfies all three.
- Put a version constant in an interface and print it through the class name.
- Add a second class to the Countable example with a different item count.
- Explain why a function should accept the small interface instead of the concrete class.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Why split a big interface into several small ones?
Answer
A class then implements only the promises it really offers
The interface segregation idea keeps each promise narrow, so a class is not forced to fake methods it cannot offer.
-
What does
interface Store extends Readable, Writablemean?Answer
A class that implements Store must implement both parents
The child interface carries every parent promise, so an implementing class fills in all of them.
-
How can an interface constant be read?
Answer
Through the interface name or through an implementing class
The constant belongs to the contract and an implementing class inherits the same value.
-
What does the Countable interface require?
Answer
A count() method that returns an int
The one method gives the language function count() the number it should report.
-
What does this program print?
<?php interface Readable { public function read(): string; } class Reader implements Readable { public function read(): string { return "hi"; } } $r = new Reader(); echo $r->read();Answer
hi
The class filled in the promise and the call returns the string.
-
What does this program print?
<?php interface Readable { public function read(): string; } interface Writable { public function write(string $text): void; } class Reader implements Readable { public function read(): string { return "hi"; } } $r = new Reader(); echo $r instanceof Writable ? "writable" : "read-only";Answer
read-only
The class promised reading only, so the instanceof check on Writable is false.
-
What does this program print?
<?php interface Readable { public function read(): string; } interface Writable { public function write(string $text): void; } interface Store extends Readable, Writable { } class Memory implements Store { public function read(): string { return "hi"; } public function write(string $text): void { } } $m = new Memory(); echo $m instanceof Readable ? "readable" : "not", " ", $m instanceof Writable ? "writable" : "not";Answer
readable writable
Implementing the child interface satisfies both parents at once.
-
What does this program print?
<?php interface Http { const OK = 200; } class Api implements Http { } echo Http::OK, " ", Api::OK;Answer
200 200
Both spellings name the same constant and print the same value.
-
What does this program print?
<?php class Bag implements Countable { public array $items = []; public function count(): int { return count($this->items); } } $bag = new Bag(); $bag->items = ["a", "b", "c"]; echo count($bag);Answer
3
count() on a Countable object calls its method, and the method counts the three items.
-
Why should a function take the small interface rather than the concrete class?
Answer
Because any class with that promise can be passed in
The parameter then describes what the function needs, so tests and other implementations fit.
The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.
Article author: Arthur Isaev