Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
183 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...