Interfaces and abstract classes
An interface is a promise of methods. An abstract class shares code and leaves the rest to the child classes.
PHP · Interfaces and abstract classesIntermediate
interface Notifier
{
public function send(string $to, string $message): bool;
}
abstract class BaseNotifier implements Notifier
{
protected function clean(string $text): string
{
return trim(strip_tags($text));
}
}
class LogNotifier extends BaseNotifier
{
public array $sent = [];
public function send(string $to, string $message): bool
{
$this->sent[] = $to . ': ' . $this->clean($message);
return true;
}
}
function alert(Notifier $n): void
{
$n->send('admin', '<b>Disk almost full</b>');
}
$log = new LogNotifier();
alert($log);interfaceabstractoop