OOP With WordPress: Why?
By: Ryan Kienstra on: January 9, 2015 in: OOP For WordPress
You’ve been programming with functions and variables.
And most of WordPress doesn’t use OOP.
So why do you need object-oriented programming?
- Store variables between functions
- Reduce arguments
- Show structure
Plugin Example
My WordPress plugin Bootstrap Widget Styling filters the markup of widgets.
Here’s how to do it without object-oriented PHP:
add_filter( 'wp_list_pages' , 'bws_filter_pages_widget_markup' ); function bws_filter_pages_widget_markup( $markup ) { return bws_filter_widget_markup( $markup , 'pages' ); } $bws_types_of_widgets_called = array(); // too exposed, can be changed by any function function bws_filter_widget_markup( $markup , $widget_type ) { $markup = bws_remove_ul_tags_if_filter_type_is_pages( $markup , $widget_type ); $markup = bws_close_ul_if_first_call_of_filter( $markup , $widget_type , $bws_types_of_widgets_called ); $markup = bws_replace_parenthesized_number_with_badge_number( $markup ); $markup = bws_remove_li_tags( $markup ); $markup = bws_add_list_group_class_to_anchor_tags( $markup ); $markup = bws_move_span_inside_anchor_closing_tag( $markup ); $markup = bws_add_closing_div_depending_on_filter_type( $markup , $widget_type ); return $markup; }
Problems With This
Can’t Store Variables
The array $bws_types_of_widgets_called
is exposed.
Any function in any plugin could change it.
It would be nice to protect it from this wide scope.
But functions can’t store variables.
Repetition
$markup
appears twice on each line.
Too Many Arguments
It’s hard to understand functions with 2 or more arguments.
Especially:
$markup = bws_close_ul_if_first_call_of_filter( $markup , $widget_type , $bws_types_of_widgets_called );
Robert Martin wrote in Clean Code that functions should take zero arguments if possible.
This is feasible with…
Object-Oriented Approach
There are 3 total arguments in the functions above.
$markup
$widget_type
$bws_types_of_widgets_called
We’ll define a class, and store these variables and functions in it.
class BWS_Filter { protected static $instance; protected static $types_of_widgets_called = array(); protected $markup; protected $widget_type; // function definitions
They’re called “properties” now.
Here’s a function inside the class:
function close_ul_if_first_call_of_filter() { if ( self::is_first_instance_of_type() ) { self::close_ul_and_add_opening_div(); } }
This function used to have 3 arguments.
Now, it doesn’t need any because they’re stored inside the class.
And the properties (variables) are all protected from outside functions changing them.
(See the entire file on GitHub)
More Advantages
Structure
You can see that the functions are used together.
OOP with WordPress makes it easy to find the code for the class.
Classes are usually in their own file.
This class is in class-bws-filter.php
.
Easy To Understand
The functions above are only used in the class.
So you only have to understand them in that context.
A function outside of a class can be called anywhere.
Next: Fundamentals Of OOP With WordPress
In the next article, you’ll learn theĀ OOP Fundamentals.
Classes are similar to functions.
Understanding functions and variables, you’ll learn the basics of OOP quickly.
And once you learn the PHP syntax for classes, you’ll be using OOP with WordPress.