OOP · Polymorphism · The Many Forms
Polymorphism in WordPress
One interface, many implementations — swap behavior without changing calling code.
Hooks are polymorphic at runtime: the same apply_filters() call runs different callbacks depending on what is registered. In object-oriented plugins, interfaces and abstract methods give you the same flexibility at compile time — email vs SMS notifiers, CSV vs REST exporters, or multiple SEO adapters behind one inventory service.
Core concept
Different classes share a common type — interface or parent class — and each implements the same method differently. Code that accepts the shared type works with any concrete implementation.
WordPress core examples
- WC_Payment_Gateway — checkout calls one API; Stripe, PayPal, and COD behave differently.
- WP_Hook — same filter name, unlimited callback implementations.
- WP_Session_Tokens — user meta vs database session handlers.
- Block render callbacks — same block name, theme or plugin supplies markup.
Notifiable services
interface Notifiable {
public function send( string $message ): void;
}
class Email_Notifier implements Notifiable {
public function send( string $message ): void {
wp_mail( $this->email, 'Alert', $message );
}
}
class Slack_Notifier implements Notifiable {
public function send( string $message ): void {
wp_remote_post( $this->webhook, [ 'body' => wp_json_encode( [ 'text' => $message ] ) ] );
}
}
function notify_user( Notifiable $notifier, string $message ): void {
$notifier->send( $message );
}4WP SEO Helper: adapter pattern
The SEO inventory reads meta through adapters for Yoast SEO and All in One SEO — the same REST endpoint and admin table work with either plugin because each adapter implements the same internal contract.