Dec 11

I’m learning how to write wordpress plugins, I’ve said that before here in my blog.  One of the ways I’m doing it is by downloading/installing existing plugins I see out there which may be interesting to me – they provide actions and capabilities to do things I want my plugin(s) to do … on a more general scale.

One thing I have noticed – almost all of these plugin authors do NOT use classes in their design.  Their .php files are full of functions, most of the time written without the

if (!function_exists('my_function')) {
  function my_function() {
    // and so on
  }
}

or

if (!function_exists('my_function')) :
  function my_function() {
    // and so on
  }
}
endif;

How do they expect to avoid naming collisions?  If they have to have a function by a certain name which probably exist in some other plugin developer’s file somewhere, then even if they use this !function_exists business they’ll end up getting the other author’s behavior for that function.

Why don’t these people just use classes! I know I’m coming at this from the java world where you have to use classes for everything, and there’s packaging to avoid naming collisions, but still – this seems like an obvious question to me. Do the modern php programmers have no experience with a real class-oriented language?

Here’s a silly example I’m doing. I have a plugin I’m developing to allow Zlatina’s web administrators to deal with their database of students and contacts. It’s called Bcc DBManager.
I created a subdirectory wp-content/plugins/bcc-dbmanager, with a file named bcc-dbmanager.php … duh. I want to hook into the activate_${plugin} (see this link for an explanation of that). Here’s a partial …

<?php
/*
Plugin Name: Bcc DBManager
Description: Blah blah
*/

$bccDBManager = new BCC_DBmanager();
add_action('activate_bcc-dbmanager/bcc-dbmanager.php',                  
         array(&$bccDBManager,'_activate'));

class BCC_DBmanager {  
  function _activate()  {    
    // do stuff    
    $this->very_common_function_name();
    // or this way
    BCC_DBmanager::very_common_function_name();
  }
  ## function with a very common name
 function very_common_function_name() {
    // do stuff
  }
}
?>

How hard is that? I certainly don’t know much about plugin development at this point. But I would have to describe the use of functions rather than class instances and methods as Code Smell.

[Edit] Since I wrote this post I found this link on Nerdaphernalia: Use Classes in your WordPress plugins to increase code portability and reduce name conflicts

  • Facebook
  • Share/Bookmark

2 Responses to “Wordpress plugins don’t commonly use classes, why?”

  1. Stephen R says:

    It seems to be more common in the WordPress plugin world to add some string to the start of the plugin’s name. Thus myplugin_some_function() and myplugin_another_function()

    P.S. — “Nerdaphernalia”

    • Kevin Hagel says:

      Is there a php-particular advantage to the use of files full of functions rather than classes in this manner?
      I’d like to avoid imposing java-think on PHP. PHP is much more useful for us here in Bulgaria at this time … and I’ve discovered wordpress to be a surprisingly flexible CMS, perfect for my needs right now. I know I need to adapt my coding to php-think … so that’s why I asked about any advantage in doing it the way I seem to see plugin authors doing it.

Leave a Reply

preload preload preload