简体   繁体   中英

Undefined variable in a class when I have defined it

Coding a new version of my own Wordpress plugin, this time using OOP to make it more readable for other collaborators. It is based on Wordpress Plugin Boilerplate and also Cuztom, to facilitate the creation of Custom Post Types.

My CPTs are created, they update well. Although I can call the hook and create my meta box from within the same function I create my CPT, I cannot create the metabox separately from another function as the object (CPT) is null .

Errors I get:

Notice: Undefined variable: person in /Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php on line 243

Fatal error: Call to a member function add_meta_box() on null in /Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php on line 243

I call the hook that way from my plugin 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' );

Here, everything works, both functions are called, the first one works and create my custom post type, the second is called without any issue but triggers an error. Here are both functions:

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 */ )
        );      

    }

So, when I use the metabox function within the create_myplugin_custom_post_types function, it works all well. As soon as I add it inside it's own create_myplugin_metaboxes function, it triggers these errors:

Notice: Undefined variable: person in /Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php on line 243

Fatal error: Call to a member function add_meta_box() on null in /Users/Lazhar/dev/wp-content/plugins/myplugin/admin/class-myplugin-admin.php on line 243

It is problematic because I have so many metabox to create that I need to do it within its own function, but $person seems undefined even if I did define it!!

_ EDIT & ADDITION: - I cannot instantiate it at __construct because it is too soon to create CPT then. __contrsuct is called too early so it then triggers other errors...

Like others have said, you need to be using $this->person because of the scope. Variables inside of functions only live inside those functions unless they are passed in and returned or passed in as a reference.

To address your comment on the OP, you don't need to instantiate it in __contruct() , but alternatively you could do something like:

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 */));
    }
}

To address your comment on Sourabh's answer. You should probably var_dump($this->person) before and/or after add_metabox() because that is the part that doesn't appear to be working based on your comment.

replace your function with this code:

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-> was missed by you, you need to use $this-> keyword to refer to same class members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM