我定义它时类中的未定义变量

时间:2015-05-28 16:27:27

标签: php wordpress-plugin wordpress-theming wordpress

编写我自己的Wordpress插件的新版本,这次使用OOP使其对其他协作者更具可读性。它基于Wordpress Plugin Boilerplate和Cuztom,以便于创建自定义帖子类型。

我的CPT已创建,它们更新得很好。虽然我可以在同一个函数中调用钩子并创建我的元框,但是我创建了CPT,因为对象(CPT) null ,我无法与另一个函数分开创建元框。

我得到的错误:

注意:未定义的变量:第243行/Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php中的人

致命错误:在第243行的/Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php中调用null上的成员函数add_meta_box()

我从我的插件类中调用了钩子:

onFinish()

在这里,一切正常,两个函数都被调用,第一个工作并创建我的自定义帖子类型,第二个被调用没有任何问题,但会触发错误。以下是两个功能:

$this->loader->add_action( 'admin_init', $plugin_admin, 'create_myplugin_custom_post_types' );
$this->loader->add_action( 'add_meta_boxes', $plugin_admin, 'create_myplugin_metaboxes' );

// Create Plugin's Menu
$this->loader->add_action( 'parent_file', $plugin_admin, 'set_current_menu' );  
$this->loader->add_action( 'admin_menu', $plugin_admin, 'setup_admin_menus' );
$this->loader->add_action( 'admin_menu', $plugin_admin, 'remove_metaboxes' );

因此,当我在 create_myplugin_custom_post_types 函数中使用元数据函数时,它可以正常工作。只要我在其自己的 create_myplugin_metaboxes 函数中添加它,它就会触发这些错误:

注意:未定义的变量:第243行/Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php中的人

致命错误:在第243行的/Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php中调用null上的成员函数add_meta_box()

这是有问题的,因为我有很多metabox来创建我需要在它自己的函数中完成它,但是$ person似乎未定义,即使我确实定义了它!

_ 编辑& ADDITION: - 我无法在__construct中实例化它,因为现在创建CPT还为时尚早。 __contrsuct过早调用,因此会触发其他错误......

2 个答案:

答案 0 :(得分:2)

与其他人一样,由于范围的原因,您需要使用$this->person。函数内部的变量仅存在于这些函数内部,除非它们被传入并作为引用返回或传入。

要解决您对OP的评论,您无需在__contruct()中对其进行实例化,但您也可以执行以下操作:

class myPlugin_Admin {
    private 
        $plugin_name,
        $version,
        $person = null;

    public function __construct ($plugin_name, $version) {
        $this->plugin_name = $plugin_name;
        $this->version = $version;
    }

    public function create_myplugin_custom_post_types () {
        // Custom Post Type: Person
        if ($this->person === null) {
            $this->person = new Cuztom_Post_Type('Person', array('supports' => array('title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', 'revisions'), 'public' => true, 'capability_type' => 'page', 'rewrite' => array('slug' => 'person'), 'menu_position' => 30, 'menu_icon' => 'dashicons-id', 'show_ui' => true, 'show_in_menu' => false, 'show_in_nav_menus' => true, 'publicly_queryable' => true, 'exclude_from_search' => false, 'has_archive' => true, 'query_var' => true, 'can_export' => true, 'hierarchical' => true,));
        }
    }

    public function create_myplugin_metaboxes () {
        if ($this->person === null) {
            $this->create_myplugin_custom_post_types();
        }
        // Person Custom Post Type's Metaboxes
        $this->person->add_meta_box('person_details', 'Details of this person', array( /* all my custom fields go here */));
    }
}

解决您对Sourabh答案的评论。您应该在var_dump($this->person)之前和/或之后add_metabox(),因为根据您的评论,这似乎不起作用。

答案 1 :(得分:0)

使用以下代码替换您的函数:

public function create_myplugin_metaboxes() {

            // Person Custom Post Type's Metaboxes

            $this->person->add_meta_box(
                'person_details',
                'Details of this person',
                array( /* all my custom fields go here */ )
            );      

        }
您错过了

$this->,您需要使用$this->关键字来引用相同的班级成员。