当我定义它时 class 中的未定义变量
Undefined variable in a class when I have defined it
编写我自己的 Wordpress 插件的新版本,这次使用 OOP 使其对其他协作者更具可读性。它基于 Wordpress 插件样板和 Cuztom,以促进自定义 Post 类型的创建。
我的 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 的空值上调用成员函数 add_meta_box()
我从我的插件中调用钩子 class:
$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' );
在这里,一切正常,两个函数都被调用,第一个有效并创建我的自定义 post 类型,第二个调用没有任何问题但触发错误。以下是两个函数:
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 函数中,它就会触发这些错误:
注意:未定义变量:第 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 的空值上调用成员函数 add_meta_box()
这是有问题的,因为我有太多的元数据要创建,我需要在它自己的函数中进行,但是 $person 似乎未定义,即使我定义了它!
_
编辑和添加:
- 我无法在 __construct 实例化它,因为那时创建 CPT 还为时过早。 __contrsuct 调用得太早,因此会触发其他错误...
用此代码替换您的函数:
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->
关键字来引用相同的 class 成员。
正如其他人所说,由于范围的原因,您需要使用 $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()
之后 and/or 之前 var_dump($this->person)
,因为根据您的评论,那部分似乎不起作用。
编写我自己的 Wordpress 插件的新版本,这次使用 OOP 使其对其他协作者更具可读性。它基于 Wordpress 插件样板和 Cuztom,以促进自定义 Post 类型的创建。
我的 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 的空值上调用成员函数 add_meta_box()
我从我的插件中调用钩子 class:
$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' );
在这里,一切正常,两个函数都被调用,第一个有效并创建我的自定义 post 类型,第二个调用没有任何问题但触发错误。以下是两个函数:
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 函数中,它就会触发这些错误:
注意:未定义变量:第 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 的空值上调用成员函数 add_meta_box()
这是有问题的,因为我有太多的元数据要创建,我需要在它自己的函数中进行,但是 $person 似乎未定义,即使我定义了它!
_ 编辑和添加: - 我无法在 __construct 实例化它,因为那时创建 CPT 还为时过早。 __contrsuct 调用得太早,因此会触发其他错误...
用此代码替换您的函数:
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->
关键字来引用相同的 class 成员。
正如其他人所说,由于范围的原因,您需要使用 $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()
之后 and/or 之前 var_dump($this->person)
,因为根据您的评论,那部分似乎不起作用。