-1

This is a continuation of this question. When the class is extended, it refers to the original class method. The echoed class name in the created page should be AnotherAdminPage which is the extended class name.

/* 
    Plugin Name: static method callback demo
*/

class AnotherAdminPage extends AdminPageClass {
}

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

class AdminPageClass {

    static function _admin_menu() {
        $class_name = get_class();
        $classinstance = new $class_name();
        return array(&$classinstance, "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">
            <p><?php echo get_class(); ?></p>
        </div>
        <?php
    }
}

It works by redefining the methods in the extended class but it becomes somewhat pointless to extend it in that case.

class AnotherAdminPage extends AdminPageClass {

    static function _admin_menu() {
        $class_name = get_class();
        $classinstance = new $class_name();
        return array(&$classinstance, "admin_menu");
    }   
    function admin_page() {
        ?>
        <div class="wrap">
            <p><?php echo get_class(); ?></p>
        </div>
        <?php
    }   
}

So is there a more elegant way of doing this?

Community
  • 1
  • 1
Teno
  • 2,582
  • 4
  • 35
  • 57

3 Answers3

1

You should use get_called_class (PHP 5.3)

EDIT :

If you don't have PHP 5.3, you should read this

PHP get_called_class() alternative

Community
  • 1
  • 1
soju
  • 25,111
  • 3
  • 68
  • 70
  • Thanks for your input. One of the servers I'm using has the PHP version of 5.2.17. Is there a workaround? – Teno Oct 10 '12 at 13:58
  • Thanks. So it's not possible with v5.2.x. I found a workaround by the way. I posted it as an answer. – Teno Oct 10 '12 at 15:02
1

Use get get_called_class instead of get_class. http://php.net/manual/en/function.get-called-class.php . Then you don't need to redefine the _admin_menu function

xCander
  • 1,338
  • 8
  • 16
  • Hmm, the PHP version of one of the server I'm using is 5.2.17, which is a free shared host. Thanks for the input. – Teno Oct 10 '12 at 13:56
-1

I found it by myself that this works.

/* 
    Plugin Name: extended class method as a callback demo
*/

class AnotherAdminPage extends AdminPageClass {
}

add_action('admin_menu', array(new AnotherAdminPage, "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">
            <p><?php echo get_class($this); ?></p>
        </div>
        <?php
    }   
}
Teno
  • 2,582
  • 4
  • 35
  • 57
  • Not really an answer since you don't use static function anymore – soju Oct 10 '12 at 15:04
  • So it is an issue related to static scope. Thanks for mentioning it. But this way, the extended class seems to refer to the own instance. This is what I wanted. But the strange thing is that without redefining the `msg()` method, it refers to the original. – Teno Oct 10 '12 at 15:08