888888.888888.88""Yb..dP"Yb..8888b..Yb..dP.88b.88....db....8b....d8.88..dP""b8..dP"Y8
88__...88__...88__dP.dP...Yb..8I..Yb.YbdP..88Yb88...dPYb...88b..d88.88.dP...`".`Ybo."
88""...88""...88"Yb..Yb...dP..8I..dY..8P...88.Y88..dP__Yb..88YbdP88.88.Yb......o.`Y8b
88.....888888.88..Yb..YbodP..8888Y"..dP....88..Y8.dP""""Yb.88.YY.88.88..YboodP.8bodP'


88b.88.888888.888888.Yb........dP.dP"Yb..88""Yb.88..dP
88Yb88.88__.....88....Yb..db..dP.dP...Yb.88__dP.88odP.
88.Y88.88"".....88.....YbdPYbdP..Yb...dP.88"Yb..88"Yb.
88..Y8.888888...88......YP..YP....YbodP..88..Yb.88..Yb

January 1, 2008

I’ve decided to kick of 2008 with a series on using object oriented PHP for WordPress plugins. I intend this to be a ground up look at using objects and so I will start at the beginning.

A lot of people come to WordPress before they come to PHP. They learn how to use tags, then how to use snippets of PHP in their themes, and then start to look at plugins. My re-introduction to PHP after several years happened just this way.

My plugin development went through three distinct phases: Classless, Static, and Object Based, the latter only when I started to look seriously at the best way to structure my plugins.

In this first part of the series I am going to look at each of these stages; the aim is to ensure that anyone reading this is at stage three, before I continue with the series.

Classless Plugins

Classless plugins are the first step in the ladder. At their simplest they are one command and one function. I have also included in the example a function designed to output content as a theme tag:

[php]
add_filter( 'the_content' , 'modify_content' );

function modify_content( $content ){
return $content . '

An extra bit of content

';

}

function ouput_something_to_theme(){
echo 'something';
}

//in the theme we can call any functions we like directly
output_something_to_theme();

This will do the job just as well as the most complex structure, but it has one major weakness: It has a very high risk of naming collisions; i.e. if these functions have the same name as functions from WordPress itself, or from another plugin then the result will be a list of errors.

This is actually quite likely as many plugins do similar tasks, such as alter content.

Static

The second stage exists purely to combat the problem of naming collisions. The page in the WordPress Codex about this was my first introduction to classes in PHP.

To combat the problem the functions are wrapped in a class and accessed statically. The functions can then be named anything without fear of conflict

[php]
class my_plugin_class
{

function modify_content( $content ){
return $content . '

An extra bit of content

';
}

function ouput_something_to_theme(){
echo 'something';
}

}

//when we add the filter we now need to include an array to tell it which class the function is in.
add_filter( 'the_content' , array( 'my_plugin_class' , 'modify_content' ) );

//in the theme a scope resolution operator would need to be used
my_plugin_class::output_something_to_theme();

There is still a minor problem of naming collisions, but it is reduced as much as it can be.

This method works and for simple plugins, like the example, perhaps there is no need to go any further than this. This is still very much a half-way house though. It introduces the class structure without making use of the benefits, and that is where stage 3, Object Based Plugins, comes in.

Object Based

At this stage, I think it is a good idea if I explain a little about objects before showing how this applies to WordPress. If you are ahead of the game at this point you may wish to skip this bit

What are objects?

In programming terms an object is a construct that contains both state and behaviour; i.e. it contains data, like a variable, but can manipulate its own data if you ask it to.

The easiest way to understand objects is to think of them in terms of real world things; first you start with the concept. I am going to use the idea of a train.

You know what a train is: it has at least four wheels, has an engine of some kind, and pulls carriages. This our basic train concept. If we see a train we can compare it to this concept to decide whether it is in fact a train.

We will call this concept a ‘class’ and our class is called ‘train’.

If we were standing on the platform wanting to go somewhere though, this class, ‘train’, is not going to help; it isn’t real, it is just an idea. So we need to make an actual train ourselves.

What we need to do is to use our class as a blueprint and construct our object according to the class. My Train, that we have created, is an object of type (or class) ‘train’

Once we have created the object we can now do with it what we want. We can repaint it, change its name, or instruct it to pull carriages.

In PHP terms we have done this:

[php]
class train{

var $wheels = 4;
var $colour = 'red';

function pull_carriages( $distance ){
//do something
return 'pulled' . $distance . 'miles';
}

}

var $my_train = new train();
$my_train->colour = 'green';
echo $my_train->pull_carriages( 5 );

This isn’t a tutorial on object oriented programming so I won’t go any further than that. A quick search will find your hundreds of websites with good tutorials, most of which I am sure are better than any I would write. I encourage you to read at least one or two if you feeling at all out of your depth at this point.

Using objects

Instantiating the classes into objects brings a number of benefits. The plugin can maintain its own state, including getting database information just once, instead of once for each function, and access it with the $this keyword. It can set this state up, and register itself with all the necessary WordPress hooks when the object is created, making it self contained, and it also helps to make the code more maintainable.

[php]
class my_plugin_class
{

var $message = '';

//the contructor (php4 style)
function my_plugin_class( $message ){
add_filter( 'the_content' , array( &$this , 'modify_content' ) );
$this->message = $message;
}

function modify_content( $content ){
return $content . '

An extra bit of content

';
}

function ouput_something_to_theme(){
echo $this->message;
}

}

//we now need to instantiate the class
$my_plugin = new my_plugin_class( 'Something' );

//in the theme we would access the object method directly.
$my_plugin->output_something_to_theme();

This is a very simple example, but it shows the basic structure of an object based plugin. The more complex the plugin becomes the clearer the benefits of using object based plugins are.

What now?

In the next part of this series I will look at using secondary objects to simplify some of the basic tasks, keep to the DRY principle (Don’t Repeat Yourself) and move the code outside of the actual plugin object.

Fun Without Cliches
Fun cliches?
12 comments
page 861
Wordpress Chat
Wp chat themes?
3 comments
page 1308
Post Image The Easy Peasy Way
Wp custom field arrau thumbnails?
26 comments
page 1065
Dont Mess With My Toot Toot
Custom content types wordpress?
15 comments
page 599
Post Image The Easy Peasy Way
Wp change post image size?
26 comments
page 1065
Using Your Own Url Shortener
Use your domain as a url shortner?
4 comments
page 1190
Dont Mess With My Toot Toot
Dont mess with my toot toot?
15 comments
page 599
Six Million Ways To Die Choose One
Simply ways to die?
14 comments
page 1128
3 Ways To Speed Up Your Blog Without A Cache Plugin
Speed up disqus?
one comment
page 1321
Updating Code Snippets Here
Fun with plugins?
no comment
page 1338
Easy Peasy Images Suggestion Roundup
Display attached images within loop?
21 comments
page 1073
Html 5 Gallery
Wordpress and html5?
6 comments
page 1305
Post Image The Easy Peasy Way
Wp get attachment?
26 comments
page 1065
Post Image The Easy Peasy Way
Post image gallery wp?
26 comments
page 1065
Why I Ditched Disqus
Better than disqus?
5 comments
page 1175
Updating Code Snippets Here
Andy dp?
no comment
page 1338
Using Wordpress As A Php Framework
Wordpress php framework?
2 comments
page 335
Post Image The Easy Peasy Way
Get images attached to a post all images?
26 comments
page 1065
Why I Ditched Disqus
Disqus vs wordpress comments?
5 comments
page 1175
Poll Daddy Reviewed
Wordpress pie chart poll?
2 comments
page 42
Html 5 Gallery
Html 5 wordpress theme gallery?
6 comments
page 1305
Html 5 Gallery
Html 5 photo album?
6 comments
page 1305
Dont Mess With My Toot Toot
Dont mess with my toot toot?
15 comments
page 599
Why You Should Try Netbeans
Function of netbeans?
6 comments
page 973
Quick N Dirty Admin Login Screen
Css login screen?
no comment
page 128
Why I Ditched Disqus
Disqus wordpress static page?
5 comments
page 1175
Fun With Sidebar Tabs Styling
Sidebar vertical tab wordpress?
2 comments
page 336
Quick N Dirty Admin Login Screen
Login page css html?
no comment
page 128
Using Your Own Url Shortener
Url for your category in wordpress?
4 comments
page 1190
3 Ways To Speed Up Your Blog Without A Cache Plugin
Speed up web with html5 cache?
one comment
page 1321
My Experience Of Flexx
Why flexx is used?
4 comments
page 1026
Wpunlimited The Ultimate Wordpress Theme
Wordpress premium theme html 5?
3 comments
page 1141
Post Image The Easy Peasy Way
Image post?
26 comments
page 1065
  1 query every 3721 seconds, updated 1 seconds ago.
Post a comment?