Joomla
Drupal
Wix
Squarespace
Wwordpress

OOP

Object-Oriented Programming

Four Principles ยท Infinite Possibilities

Scroll to explore
Encapsulation
Abstraction
Inheritance
Polymorphism

What is OOP?

Object-Oriented Programming organizes code around objects rather than functions and logic. Each object has its own data and behavior โ€” like a whale that knows how to swim, eat, and breathe.

In WordPress development, OOP helps you build maintainable, reusable code. These four principles โ€” Encapsulation, Abstraction, Inheritance, Polymorphism โ€” are the foundation for SOLID.

Core Idea

OOP groups related data and behavior into objects. Instead of procedural functions that pass data around, you create classes that encapsulate both. WordPress core uses OOP extensively โ€” WP_Widget, WP_Query, WP_Post are all classes.

WordPress OOP Examples

See how OOP applies across different WordPress project sizes

Landing pages, blogs, small business sites

Use simple classes for contact forms, custom post types, or theme options. Encapsulation keeps your data safe; inheritance lets you extend WP_Widget.

Bash
class Theme_Options {
    private $options = [];
    public function get($key) { return $this->options[$key] ?? null; }
    public function save($key, $value) {
        $this->options[$key] = sanitize_text_field($value);
        update_option('my_theme_options', $this->options);
    }
}

WooCommerce, membership platforms, multi-site

Full OOP with interfaces, dependency injection, and layered architecture. Polymorphism for payment gateways; abstraction for storage backends.

Bash
interface Payment_Gateway_Interface {
    public function charge($amount, $token);
}
class Stripe_Gateway implements Payment_Gateway_Interface { ... }
class PayPal_Gateway implements Payment_Gateway_Interface { ... }

Built-in classes you extend every day

Inheritance โ€” extend to create custom widgets

WP_Query

Encapsulation โ€” hides SQL, exposes simple API

WP_REST_Controller

Abstraction โ€” define routes without HTTP details

WC_Payment_Gateway

Polymorphism โ€” each gateway implements same interface

The Four Pillars in Detail

Encapsulation

The Protected Whale

Like a whale that keeps its vital organs safely inside, encapsulation shields internal data from the outside world, exposing only what is necessary through a controlled interface.

Bash
class UserProfile {
    private $data;
    public function __construct($data) { $this->data = $data; }
    public function get_display_name() { return $this->data['display_name']; }
    private function get_password_hash() { return $this->data['password']; }
}

Abstraction

The Deep Whale

A whale dives deep into the ocean, hiding complexity beneath the surface. Abstraction reveals only what matters โ€” hiding the inner workings and showing a simple interface.

Bash
abstract class Payment_Gateway {
    abstract public function process($amount);
}
class Stripe extends Payment_Gateway {
    public function process($amount) { return $this->stripe_api->charge($amount); }
}

Inheritance

Mother & Calf

A mother whale passes her traits to her calf. In OOP, child classes inherit properties and methods from parent classes, extending and specializing behavior without rewriting everything.

Bash
class Base_Widget extends WP_Widget {
    protected function cache($output) { set_transient($this->id, $output); }
}
class Popular_Posts extends Base_Widget {
    public function widget($args, $instance) {
        $posts = get_posts(['orderby' => 'comment_count']);
        $this->cache($this->render($posts));
    }
}

Polymorphism

The Many Forms

Like whales that adapt their behavior to different environments, polymorphism lets objects share the same interface but behave differently depending on their type.

Bash
interface Notifiable { public function send($msg); }
class Email_Notifier implements Notifiable {
    public function send($msg) { wp_mail($this->email, 'Alert', $msg); }
}
function notify(Notifiable $n, $m) { $n->send($m); }