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

November 8, 2007

I think you should give objects a try, for more reasons than just avoiding naming collisions. Here’s why.

I think I’m a pretty typical Wordpress user. I learned a lot of my PHP by playing with themes to get them to do things they didn’t by default. So when I decided to write a plugin I pretty much hacked it together using the guidance that was available.

The only real introduction I had to objects when I started writing plugins was a brief piece about avoiding naming collisisons. None of the guidance seemed to offer more than that so I never really gave it much thought.

Typically my plugins were structured something like this:

[php]
//plugin headers

class myClass {
function myFunction() {
//do stuff
}

}

add_action('wp-head',array('myClass','myFunction'));

There isn’t anything wrong with this, but classes can do so much more once they are instantiated as an object. Typically I now structure my plugins more like this:

[php]

class myClass(){

function myClass(){
//php 4 compliant constructor

add_action('wp-head',array(&$this,'myFunction'));

}

function myFunction(){

//do stuff

}

}

if (class_exists('myClass')) {
$myClass = new myClass();
}

So what are the advantages? The real benefits are simply those gained by using object oriented code versus procedural code. The code becomes easier to maintain and easier to reuse the individual functions in other classes.

It can also be useful for abstracting the administrative content away from the actual code you are modifying; for example, I have found it useful to load all of the plugin options using the constructor and keep them in an internal variable for later use by whichever functions happen to need it using something like this:

[php]
class myClass{

function myClass(){

$this->options = get_option('myPluginOptions');
add_filter('the_content',array(&$this,'myFunction'));

}

function myFunction($content){

if ($this->options['wrap-content'] == true) {
return '

'.$content.'

';
} else {
return $content;
}

}
}

So, if you are not already instantiating your classes, then I suggest you consider it. You might even enjoy it.

Html 5 Gallery
Wordpress html5?
6 comments
page 1305
Quick N Dirty Admin Login Screen
Login screen css?
no comment
page 128
What Wordpress Workflow Needs
Word press workflow?
3 comments
page 1226
How To Add Sidebars To A Theme
Wordpress thematic modify sidebars?
10 comments
page 1053
Post Image The Easy Peasy Way
Wordpress theimage?
26 comments
page 1065
Six Million Ways To Die Choose One
Ways to die without noticing?
14 comments
page 1128
How To Add Sidebars To A Theme
How to add a sidebar to a theme?
10 comments
page 1053
Using Your Own Url Shortener
Htaccess tiny url shortner?
4 comments
page 1190
Photoshop Design Framework
Photoshop framework?
3 comments
page 296
Creating Custom Urls
Custom url for wordpress page?
6 comments
page 80
Photoshop Design Framework
Framework in photoshop?
3 comments
page 296
What Wordpress Workflow Needs
Wordpress workflow?
3 comments
page 1226
Html 5 Gallery
Wordpress html5?
6 comments
page 1305
Using Your Own Url Shortener
Build your own url shortener?
4 comments
page 1190
My Experience Of Flexx
Cant upload image to flexx wordpress theme?
4 comments
page 1026
Post Image The Easy Peasy Way
Wpget post image?
26 comments
page 1065
Post Image The Easy Peasy Way
Get postattachments?
26 comments
page 1065
Html 5 Gallery
Html5 plugin wordpress?
6 comments
page 1305
3 Ways To Speed Up Your Blog Without A Cache Plugin
Wordpress cache disqus?
one comment
page 1321
Questions About Habari For Wordpress Users
Habari or wordpress?
6 comments
page 424
Custom Hooks For Admin Pages
Wordpress post form admin pages?
one comment
page 430
Updating Code Snippets Here
Yb fun blogs?
no comment
page 1338
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Html 5 Gallery
Html5 photo gallery?
6 comments
page 1305
Using Wordpress As A Php Framework
Using wordpress as a framework?
2 comments
page 335
Using Your Own Url Shortener
Rewriterule shorten url?
4 comments
page 1190
Post Image The Easy Peasy Way
Finding attached image with post in wordpress?
26 comments
page 1065
Wordpress 25 Exif Fields
Wordpress exif plugin?
12 comments
page 230
Html 5 Gallery
Html 5 photo gallery?
6 comments
page 1305
Post Image The Easy Peasy Way
Wp image attached linkable?
26 comments
page 1065
My Experience Of Flexx
How to edit flexx theme wordpress?
4 comments
page 1026
Post Image The Easy Peasy Way
List image attachments wordpress?
26 comments
page 1065
Post Image The Easy Peasy Way
Wp get first image for post?
26 comments
page 1065
  1 query every 1056 seconds, updated 1 seconds ago.
Wednesday, 7am
 __
(__)
   `

 Kaloyan K. Tsvetkov

Nice article. I myself also use a objects for my WP plugins. The only difference is that I kickstart them in a more spartan way – just calling something like this:

Class wp_something {

}
new wp_something;

because, actually, I only need to run the constructor of the class – I do not need a variable storing the created object ;)

Saturday, 9am
 __
(__)
   `

 Andrew Rickmann

Oops, in all the time I’ve been using WordPress I hadn’t noticed. Thanks John.

Saturday, 3am
 __
(__)
   `

 johnbillion

Andrew, I know I should be worrying about more pressing issues, but you know it’s WordPress (with a capital P) and not Wordpress don’t you?

That caveat aside, this is a nice little site you’ve got here. I’ll subscribe once you change the title ;-)

Keep up the good work.

Friday, 8pm
 __
(__)
   `

 Andrew Rickmann

Fullo, I did know about the PHP 5 fallback which is why I felt secure in using it; however, it is something that needs to be considered as PHP 6 won’t have it any more.

I guess it really is a case of deciding how long the plugin is likely to be around, and whether you want to support it later in its life to upgrade to PHP 6.

Having said that many hosts are only now getting up to PHP 5. If PHP 6 won’t be backward compatible then I guess you need to wonder how long it will really be until PHP 6 is offered. We could be facing up to 5 years before it becomes well supported.

That’s a long time for a plugin.

Friday, 4pm
 __
(__)
   `

 fullo

as I know call_user_function_array can be used to call statically an object method.

But I don’t know if there are some valid reasons to not use __contruct($args) { $this->myClass($args) ); as you suggest.

Btw, I’ve studied a little more and I’ve discovered that

“To ease the transition from PHP 4, if PHP 5 cannot find a method names __construct() within your object hierarchy, it revers to the PHP 4 constructor naming scheme and searches accordingly” (from “Upgrading to PHP5″ Adam
Trachtenberg (O’Reilly))

however, IMHO, this should not be used. Especially if you work with object that extends other object. Because it should be very easy to break your code simply changing the name of the parent class [parent::MyParent ==> parent::NewParentName ].

Friday, 10am
 __
(__)
   `

 Andrew Rickmann

Thanks Fullo. I did consider doing something like this but my desire to simplify often gets the better of me.

Is there a benefit to using call_user_function over simply calling myClass from within the __construct method?

Friday, 9am
 __
(__)
   `

 fullo

nice tutorial, but instead of use a php4-only object you should create a php5, or crosscompatible, object.

ie.
class myClass(){

function __construct(){
// do something
}

function myClass($args){
//php 4 compliant fake constructor
$args = func_get_args();
call_user_func_array(array(&$this, ‘__construct’), $args);

}