WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Good practice for oop & php5 plugin programming (8 posts)

  1. olivier89
    Member
    Posted 2 years ago #

    Hello everyone, i'm used to php but i'm pretty new to WP, and Object Oriented Programming (using classes and stuff :D ).
    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 :D on activation, deactivation and uninstall
    The problem is how to pass object stuff inside.

    I used the skeleton used here :
    http://wordpress.stackexchange.com/questions/25910/uninstall-activate-deactivate-a-plugin-typical-features-how-to/25979#25979
    as advised in http://codex.wordpress.org/Function_Reference/register_activation_hook

    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.

  2. wpgwiggum
    Member
    Posted 2 years ago #

    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.

  3. olivier89
    Member
    Posted 2 years ago #

    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.
    Thanks.

  4. Mark / t31os
    Moderator
    Posted 2 years ago #

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

    http://php.net/manual/en/language.oop5.static.php

    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
  5. olivier89
    Member
    Posted 2 years ago #

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

    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 ?

  6. olivier89
    Member
    Posted 2 years ago #

    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).

  7. Mark / t31os
    Moderator
    Posted 2 years ago #

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

    http://wordpress.org/about/requirements/

    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>';
    	}
    	return;
    }

    Here's a link to the source of my plugin, incase you're curious to see more.
    http://svn.wp-plugins.org/put/trunk/put.php

  8. olivier89
    Member
    Posted 2 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic