Use static or non-static methods with add_action hooks?

[ad_1]

Hi,

what do you feel is better style or better practice to hook into WP actions?

Using dynamic Classes/Methods to hook into actions or using static Classes/Methods?

Version A, dynamically:

new MetaboxPost();
class MetaboxPost
{
    private $id            = 'plugin_metbox_name';
    private $title         = 'Meine Metabox';
    private $callback      = [ $this, 'render_metabox' ];
    private $screens       = ['post','event'];
    private $position      = 'side';
    private $priority      = 'low';

    function __construct( )
    {
        add_action( 'add_meta_boxes', [ $this, 'add_meta_box' ] );
    }

    public function add_metabox()
    {
        add_meta_box( $this->id, $this->title, $this->callback, $this->screens, $this->position, $this->priority );
}

    public function render_metabox()
    {
        echo "<h1>My Metabox HTML</h1>";
}
}

Version B, statically:

add_action( 'add_meta_boxes', ['MyMetaBox', 'add_metabox'] );
class MyMetaBox
{
    private static $ID              = 'my_metabox_id';
    private static $title           = 'My Metabox Title';
    private static $render_callback = ['MyMetaBox', 'render_metabox'];
    private static $show_on_screens = [ 'post' ];
    private static $position        = 'side';
    private static $priority        = 'default';

    public static function add_metabox()
    {   
        add_meta_box( self::$ID, self::$title, self::$render_callback, self::$show_on_screens, self::$position, self::$priority );
    }

    public static function render_metabox()
    {
        echo "<h1>My Metabox HTML</h1>";
    }
}

I feel like Version A creates a more "closes within itself" organization of code, but it kinda annoys me that I have to new construct an Object just to hook into the actions.

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer