6

I'm looking for a way to pass a class method to a call-back function parameter.

I usually use create_function() as follows but I've heard it is slow and makes it difficult to debug.

add_action('init', create_function('', '$o = new AdminPageClass;'));
class AdminPageClass {

    function __construct() {
        add_action('admin_menu', array(&$this, 'admin_menu'));  
    }
    function admin_menu() {
        add_options_page(
            'Sample Admin Page Class', 
            'Sample Admin Page Class', 
            'manage_options',
            'sample_admin-page_class', 
            array(&$this, 'admin_page'));
    }
    function admin_page() {
        ?>
        <div class="wrap">
            <h1>Hi there</h1>
            <p>Hello World!</p>
        </div>
        <?php
    }
}

Of course, it can be done with an extra function like this; however, I'd like to do it without it if possible.

add_action('init', 'load_admin_page_class');
function load_admin_page_class() {
    $o = new AdminPageClass;
}

Also, instantiating the class object prior to it makes it possible but it creates an extra line as well.

$o = new AdminPageClass;
add_action('admin_menu', array(&$o, 'admin_menu'));

class AdminPageClass {

    function admin_menu() {
        add_options_page(
            'Sample Admin Page Class', 
            'Sample Admin Page Class', 
            'manage_options',
            'sample_admin-page_class', 
            array(&$this, 'admin_page'));
    }
    function admin_page() {
        ?>
        <div class="wrap">
            <h1>Hi there</h1>
            <p>Hello World!</p>
        </div>
        <?php
    }
}

I always define classes in separate files so I prefer the first method which reduces lines in the main plugin file. But as mentioned, use of create_function() should be avoided.

I appreciate your information.

Teno
  • 2,582
  • 4
  • 35
  • 57

2 Answers2

13

I found that there was no need to create a static method at all.

add_action('admin_menu', array(new AdminPageClass, "admin_menu"));

class AdminPageClass {

    function admin_menu() {
        add_options_page(
            'Sample Admin Page Class', 
            'Sample Admin Page Class', 
            'manage_options',
            'sample_admin_page_class', 
            array(&$this, 'admin_page'));
    }
    function admin_page() {
    ?>
        <div class="wrap">
            <h1>Hi there</h1>
            <p>Hello World!</p>
        </div>
    <?php
    }       
}
Teno
  • 2,582
  • 4
  • 35
  • 57
7

Create a static method which returns the array containing the class instance and the method name.

/* 
    Plugin Name: static method callback demo
*/

add_action('admin_menu', AdminPageClass::_admin_menu());

class AdminPageClass {

    static function _admin_menu() {
        $class_name = get_class();
        $classinstance = new $class_name();
        return array(&$classinstance, "admin_menu");
    }
    function admin_menu($instantiate=false) {
        add_options_page(
            'Sample Admin Page Class', 
            'Sample Admin Page Class', 
            'manage_options',
            'sample_admin-page_class', 
            array(&$this, 'admin_page'));
    }
    function admin_page() {
        ?>
        <div class="wrap">
            <h1>Hi there</h1>
            <p>Hello World!</p>
        </div>
        <?php
    }
}
  • I found a problem. When the class is extended, it does not refer to the extended class but the original class. I asked a new question: http://stackoverflow.com/questions/12820728/passing-extended-class-method-as-a-call-back-function-in-wordpress – Teno Oct 10 '12 at 13:40