mark.watero.us

Wordpress stuff, a statistics plugin, and jello

PHP 4 is dead, long live PHP 5!

leave a comment

There are quite a few reasons why developers may choose to use PHP 5 when building their applications. For example, PHP 5’s implementation of object oriented programming is much more robust and in depth than version 4’s almost an afterthought of an implementation. For another, there’s a plethora of new functions that you may find necessary to accomplish a certain task.

Amongst the list of new features, functions, constructs and other ’stuff’ is the simple fact that PHP 4 has been declared dead by its own development team.

How does this affect Wordpress?

The problem facing Wordpress plugin developers is that despite PHP 5 being available for as many years as it is versioned, and PHP 4 being declared dead, is the fact that a lot of hosts out there still use it as their default installation of PHP. In my personal opinion they should all be chastised for this, but in their defense most do have the option to upgrade to PHP 5. You just have to ask (nicely).

If you decide you need to incorporate PHP 5 specific functionality in your plugin, you’re going to have to also allow it to die gracefully for those who are still running under PHP 4.

Trust me, no matter how much you make note of it in your Readme, or display notifications on your web site, people will still be confounded by the fatal error that they are faced with when they attempt to activate your plugin. Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in /plugin/plugin.php on line 29 means absolutely nothing to somebody who isn’t a developer, and cannot by itself point them in the right direction to fixing their newly discovered situation.

How to point them in the right direction

There are a few ways to go about this, and I’ll show you one of the most direct methods here.

The best way to ensure that your end users won’t install and activate your plugin, only to run into some previously unknown problem a day later (and after spending all that time configuring and implementing it) is to check during the activation process. If the check is run there, and halts activation, you can inform your user of the problem and even help them out in fixing it without ever having to leave your couch.

So therefore the first step, if you didn’t already have one, is to add an activation hook to your plugin. From within this hook we’re going to run a version comparison on the users installed copy of PHP, against the version you are expecting to find.

If you already have an activation hook for your plugin, all you need to add is lines 5 and 6 at the top of your existing callback function.

1
2
3
4
5
6
7
8
register_activation_hook( __FILE__, 'myplugin_activate' );
 
function myplugin_activate() {
 
	if ( version_compare( phpversion(), '5.0', '<' ) )
		wp_die( '<strong>We apologize, but kStats Reloaded requires PHP 5 or above for normal operation.</strong><br />PHP 4 is outdated <a href="http://www.php.net/archive/2007.php#2007-07-13-1">by over 2 years now</a>, and you should contact your host and request that they upgrade your server immediately.' );
 
}

Feel free to get more creative, but at the very root of the situation this will do the trick.

Checking for the existence of…

The check we’ve just run is very simple in nature, due to the fact that I’m only using PHP 5 OOP and not a specific function or functionality that was introduced in a later version (such as 5.1.0, 5.2.0, etc). You can improve on this by adding specific checks with function_exists() or any manner that you see fit.

Related Blog Posts

You may find the following posts on other blogs to be helpful in clarifying this article. Both are well written and explore more in depth methods of providing a solution to the situation.

Nothing But Words: How to Make a PHP 5 WordPress Plugin Die Gracefully in PHP 4
Nerdlife: Wordpress Plugin Installation Hackery

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • NewsVine
  • Reddit
  • StumbleUpon
  • Technorati
  • Twitter

Written by mark

November 2nd, 2009 at 3:19 pm

Posted in Plugins

Tagged with ,

Leave a Reply