OOP Inheritance For WordPress

By: Ryan Kienstra on: January 20, 2015  in: OOP For WordPress

oop inheritance iconOOP inheritance allows powerful, reusable code.

If you haven’t already, you might read my guides:

Rationale: Reusing Code

Programming is largely about reusing code.

Functions execute the same code every time they’re called.

The only difference is the arguments.

But inheritance in object-oriented programming is much more flexible.

You can set a class that inherits everything from another class.

And change whatever you’d like.

OOP Inheritance Example In PHP

The WP_Widget class below registers widgets in WordPress.

It has 19 methods and 370 lines of code.

(See the full class)

To register your own widget, create a class that inherits from WP_Widget.

And override 4 of its functions.

The class will keep the other 15 functions from its parent.

Here’s a widget class in my Adapter Post Preview WordPress plugin:

class Adapter_Post_Widget extends WP_Widget {

        public function __construct() {
                $options = array( 
                        'classname'   => 'adapter-post-preview' ,
                        'description' => __( 'Show a carousel of recent posts, or a selected one' , 'adapter-post-preview' ) ,
                );
                parent::__construct( 
                        'adapter_post_preview' , 
                        __( 'Adapter Post Preview' , 'adapter-post-preview' ) , 
                        $options 
                );
        }

        public function form( $instance ) {
                // code omitted 
        }

        public function update( $new_instance , $previous_instance ) {
                //code omitted
        }

        public function widget( $args , $instance ) { 
                // code omitted
        }

}

To inherit from a class, add extends followed by the class name.

class Adapter_Post_Widget extends WP_Widget

Overriding Methods

With OOP inheritance, you can replace any method of the parent class.

Write a method of the same name in the inheriting class.

For example, WP_Widget has a __construct method.

But in Adapter_Post_Widget, I wrote a __construct method.

So calling __construct on Adapter_Post_Widget will use this new method.

Adapter_Post_Widget overrides 4 WP_Widget functions: __construct, form, update, and widget.

Using Parent Methods

Even if you’ve overridden a parent method, you can call it.

In the example above, the __construct method calls parent::__construct.

It calls the __construct method of its parent, WP_Widget.

Drawback: Harder To Change

Child classes depend on their parents.

So small changes can affect the whole hierachy.

And many levels of inheritance can make change impossible.

In Effective Java, Joshua Bloch recommends composition over inheritance.

In this, you store an instance of the parent class as a property of the inheriting class.

The inheriting class calls the methods of its parent.

But Bloch adds that inheritance is safe if the same programmers control the code.

In WordPress, using only one level of inheritance is a good practice.

Should A Class Inherit?

Joshua Bloch explains that classes should only inherit if they are a version of the parent.

For example, the Adapter_Post_Widget class is a version of WP_Widget.

The Adapter Post Widget is a widget.

This is called an “is-a” relationship.

A sports car is a version of a car.

But a class for a transmission should not inherit from a class for a car.

A car has a transmission, but a transmission isn’t a car.

Benefits

OOP inheritance promotes

  • Code reuse
  • Flexibility

Limited use of inheritance will help you solve complex problems.

How do you plan to use inheritance?

Do you have any questions?

Leave a comment below.

  • This field is for validation purposes and should be left unchanged.

Leave a comment

Get Free Updates

  • This field is for validation purposes and should be left unchanged.