编写我自己的 Wordpress 插件的新版本,这次使用 OOP 使其对其他协作者更具可读性。它基于 Wordpress 插件样板和 Cuztom,以促进自定义帖子类型的创建。
我的 CPT 已创建,更新良好。虽然我可以在创建 CPT 的同一个函数中调用 Hook 并创建元框,但我无法从另一个函数中单独创建元框,因为对象 (CPT) null。
我得到的错误:
注意: undefined variable :person 在/Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php 243 行
fatal error :在/Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php 第 243 行调用成员函数 add_meta_box() 为空
我从我的插件类中调用钩子(Hook):
$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' );
在这里,一切正常,两个函数都被调用,第一个有效并创建我的自定义帖子类型,第二个被调用没有任何问题但触发错误。这是两个函数:
class myPlugin_Admin {
private $plugin_name;
private $version;
var $person;
// also tried protected $person, private, public, etc...
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
$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() {
// Person Custom Post Type's Metaboxes
$person->add_meta_box(
'person_details',
'Details of this person',
array( /* all my custom fields go here */ )
);
}
因此,当我在 create_myplugin_custom_post_types 函数中使用 metabox 函数时,它运行良好。一旦我将它添加到它自己的 create_myplugin_metaboxes 函数中,它就会触发这些错误:
注意: undefined variable :person 在/Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php 243 行
fatal error :在/Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php 第 243 行调用成员函数 add_meta_box() 为空
这是有问题的,因为我有太多的元数据要创建,我需要在它自己的函数中创建它,但是 $person 似乎未定义,即使我定义了它!
_ 编辑和添加: - 我无法在 __construct 实例化它,因为那时创建 CPT 还为时过早。 __contrsuct 被调用得太早,因此它会触发其他错误...
最佳答案
正如其他人所说,由于范围的原因,您需要使用 $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 的回答的评论。您可能应该在 add_metabox()
之前和/或之后 var_dump($this->person)
因为根据您的评论,那部分似乎不起作用.
关于php - 当我定义它时类中 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30512184/