Home » Php » php – Call non static method inside static method from within the same class

php – Call non static method inside static method from within the same class

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m still learning PHP and have a small question – I’m wanting to structure my methods out better and I’m tossing the post_type and taxonomy stuff inside it’s own methods but I wanted to see if it’s possible to achieve this.

Can I call public static function register_taxonomy() without it being static and calling it inside the register() method? How I have it now works great, but I want to minimize as many static functions as possible. The Recipe:: is the current class that I’m in, which is basically self::.

Is it possible to call the taxonomy method inside the register method without it being static?

/**
 * Register all method
 */
public static function register()
{
    Recipe::register_taxonomy();
}

/**
 * Register Taxonomy
 */
public static function register_taxonomy()
{
    add_action('init', function() {
        $labels = [
            'name' => _x('Categories', 'taxonomy general name', 'textdomain'),
            'singular_name' => _x('Category', 'taxonomy singular name', 'textdomain'),
            'search_items' => __('Search Categories', 'textdomain'),
            'all_items' => __('All Categories', 'textdomain'),
            'parent_item' => __('Parent Category', 'textdomain'),
            'parent_item_colon' => __('Parent Category:', 'textdomain'),
            'edit_item' => __('Edit Category', 'textdomain'),
            'update_item' => __('Update Category', 'textdomain'),
            'add_new_item' => __('Add New Category', 'textdomain'),
            'new_item_name' => __('New Category Name', 'textdomain'),
            'menu_name' => __('Categories', 'textdomain'),
        ];

        $args = [
            'hierarchical'      => true,
            'labels'            => $labels,
            'show_ui'           => true,
            'show_admin_column' => true,
            'show_in_rest'      => true, // Show the categories
            'query_var'         => true,
            'capabilities'      => [
                'manage_terms' => 'manage_categories',
                'edit_terms' => 'manage_categories',
                'delete_terms' => 'manage_categories',
                'assign_terms' => 'edit_posts',
            ],
            'rewrite' => [
                'slug' => 'recipe-categories',
            ],
        ];
        register_taxonomy('recipe-categories', ['recipe'], $args);
    });
}
How to&Answers: