0

I am writing a plugin that uses a class in order to generate custom post types. I am using PHPStorm and it is reporting that some variables are undefined.

For Example...

class Custom_Post_Type {
        public $post_type_name;
        public $post_type_args;
        public $post_type_labels;

        /** Constructor */
        public function __construct( $name, $args = array(), $labels = array() ) {

            // Set Variables
            $this->$post_type_name      = strtolower( str_replace( ' ','_', $name) );
            $this->$post_type_args      = $args;
            $this->$post_type_lables    = $labels;

PHPStorm is reporting that $post_type_name, $post_type_args, and $post_type_labels are undefined and thus are errors.

I am also getting the same error with this section of code too:

public function add_taxonomy( $name, $args = array(), $labels = array() ) {

if( ! empty( $name ) ) {

    // Get Post Name
    $post_type_name = $this->post_type_name;

    // Taxonomy Properties
    $taxonomy_name   = strtolower( str_replace( ' ', '_', $name ) );
    $taxonomy_labels = $labels;
    $taxonomy_args   = $args;

    if( ! taxonomy_exists( $taxonomy_name ) ) {

        // Capitalize words and make them plural
        $name   = ucwords( str_replace( '_', ' ', $this->post_type_name ) );
        $plural = self::pluralize( $name );

        // Set labels with some defaults and merge in overrides
        $labels = array_merge(

            // Defaults
            array(
                'name'               => _x( $plural, 'Post Type General Name' ),
                'singular_name'      => _x( $name, 'Post Type Singular Name' ),
                'search_items'       => __( 'Search', $plural ),
                'parent_item'        => __( 'Parent ' . $name ),
                'parent_item_colon'  => __( 'Parent ' . $name . ':' ),
                'edit_item'          => __( 'Edit' . $name ),
                'update_item'        => __( 'Update' . $name ),
                'add_new_item'       => __( 'Add New' . $name ),
                'new_item_name'      => __( 'New' . $name . ' Name' ),
                'menu_name'          => __( $name ),
            ),

            // Overrides
            $taxonomy_labels
        );

        // Set default arguments and merge in overrides
        $args = array_merge(

            // Default Values
            array(
                'label'             => $plural,
                'labels'            => $labels,
                'public'            => true,
                'show_ui'           => true,
                'show_in_nav_menus' => true,
                '_builtin'          => false,
            ),

            // Overrides
            $taxonomy_args
        );

        // Create Taxonomy and Add it to the Post Type
        add_action( 'init',
            function() use( $taxonomy_name, $post_type_name ) {
                register_taxonomy( $taxonomy_name, $post_type_name, $args );
            }
        );

        } else {

            // Add Already-Existing Taxonomy to Post Type
            add_action( 'init',
                function() use( $taxonomy_name, $post_type_name ) {
                    register_taxonomy_for_object_type( $taxonomy_name, $post_type_name );
                }
            );
        }
    }
}

PHPStorm is telling me that $args in the register_taxonomy() statement is also not defined.

My question is are these variables actually undefined and this this plugin will blow up in my face when I enable it for testing or is it just a PHPStorm bug/quirk? Obviously this is not something I can test right away but I want to clear these errors before going any further.

1

Looks like some minor confusion here and syntax issues.

Bad: You're including the $ sign when setting property values.

// Set Variables
$this->$post_type_name      = strtolower( str_replace( ' ','_', $name) );
$this->$post_type_args      = $args;
$this->$post_type_lables    = $labels;

Good: Omit the $ once you've declared them.

// Set Variables
$this->post_type_name      = strtolower( str_replace( ' ','_', $name) );
$this->post_type_args      = $args;
$this->post_type_lables    = $labels;

Good: This is good too, you have this right.

public $post_type_name;
public $post_type_args;
public $post_type_labels;

Regarding $args. It's difficult to tell where the second snippet fits in exactly, but assuming it's in the constructor, I'm not seeing an $args variable within the context of your closure. You'll need to use($args) if that variable is from the outer scope.

add_action( 'init',
    function() use( $taxonomy_name, $post_type_name, $args ) {
        register_taxonomy( $taxonomy_name, $post_type_name, $args );
    }
);

See also: PHP closures, inheriting variables from parent scope

  • It's in a separate function. I'm updating the question with the full function now. – Cedon Nov 16 '17 at 21:42
  • The full method has been added to the original question – Cedon Nov 16 '17 at 21:52
  • Actually, I found my problem. I missed injecting it in the use() function. – Cedon Nov 16 '17 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.