Support » Plugins » Hacks » Good practice for oop & php5 plugin programming

  • Resolved olivier89


    Hello everyone, i’m used to php but i’m pretty new to WP, and Object Oriented Programming (using classes and stuff 😀 ).
    i get more or less the concepts but i lack practice and maybe class design logic.

    I wish to write a plugin, the best practice way possible.
    This is just the skeleton, but i can’t get it work.

    I want my plugin to do things 😀 on activation, deactivation and uninstall
    The problem is how to pass object stuff inside.

    I used the skeleton used here :
    as advised in

    If you take the time to look at my code sofar, obviously i got an error when i call class method do_activate() because i call $this
    But my noob question would be then, what’s the use of doing class if i have to use only static methods ?

    So my questions :
    – how can i add options / create tables on activation based on this class ?
    – Is it possible to have (non static) properties i can use inside my class, like table names, plugin version, options ? If yes, how. I mean for instance, my plugin tables names should be properties and not global since they are intended to be used only in the plugin.

    Thanks in advance for any help or advice.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hey,

    Take a look at my simple plugin wp-scheduled-styles

    It is built in a OO style, even a bit cleaner than the skeleton that you have used.

    I think one of your problems may be the static declaraion of some of your methods. static-ness is not required! If you have more questions, feel free to contact me directly.

    Thanks a lot for your answer, i’m going to check your plugin in detail.
    But you say i use static declarations, but in the link from the codex i wrote in the first post, static seems mandatory for some methods :

    If your plugin is desgined as a class write as follow:
    class MyPlugin {
         static function install() {
                // do not generate any output here
    register_activation_hook( __FILE__, array('MyPlugin', 'install') );

    But isn’t a no attribute before a method equivalent to static?

    Can’t i use private methods and properties to tighten up the security ?

    Anyway, i’ll study your plugin and contact you if i have any question.

    I’d suggest having a read of the following page to understand the difference between static and non-static functions.

    Without static:

    class example {
        function example_func() {
            echo 'Hello world';
    example::example_func(); // Won't work
    $some_var = new example;
    $some_var->example_func(); // Will work

    With static:

    class example {
        static function example_func() {
            echo 'Hello world';
    example::example_func(); // Will work

    ouch ! No I know how deep i lack knowledge with oop 😀

    Of course you’re right, and my knowledge of wp syntax is poor too. So as i asked : is it forbidden in WP syntax to use public/private (php5 attributes) in a WP plugin declaration ? Or is this question just useless, and i should always use no attribute or static ?

    Sorry if my questions are basic. But as i stated in my first post i’d like to write plugin, using best practice, in WP of course, but also in oop (php5) too, with security concerns (attribute types).

    I require PHP 5 for the plugins i’ve released and WordPress does also now require PHP 5 and MySQL 5.

    So feel free to write your code using PHP 5 methods, i certainly do, it’s pretty easy to check for PHP 5 inside your plugin, i do it like this inside my Post UI Tabs plugin..

    if( version_compare( PHP_VERSION, '5.0.0', '<' ) ) {
    	add_action( 'admin_notices', 'put_version_require' );
    	function put_version_require() {
    		if( current_user_can( 'manage_options' ) )
    			echo '<div class="error"><p>The Post Tabs UI plugin requires at least PHP 5.</p></div>';

    Here’s a link to the source of my plugin, incase you’re curious to see more.

    excellent, this is exactly what i was looking for. I’m going to dissect your code now.
    Thanks to both of you Adam and Mark for your help.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Good practice for oop & php5 plugin programming’ is closed to new replies.