DISQUS

Fun with WordPress: Using Objects for Wordpress plugins

  • fullo · 2 years ago
    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);

    }
  • Andrew Rickmann · 2 years ago
    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?
  • fullo · 2 years ago
    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 ].
  • Andrew Rickmann · 2 years ago
    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.
  • johnbillion · 2 years ago
    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.
  • Andrew Rickmann · 2 years ago
    Oops, in all the time I've been using WordPress I hadn't noticed. Thanks John.
  • Kaloyan K. Tsvetkov · 2 years ago
    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 ;)