Must activation/deactivation functions in a class be static?

Depends the way you want to implement it. The static is used because you don’t have to instantiate the class to use the functions of the class. It’s up to you. I generally would do:

<?php

/*it's good practice that every class has its own file. So put it in for example 'classes/class-hooks.php' and require it properly in your plugin file. */

class Hooks{
    static function activation(){
       //the code you want to execute when the plugin activates
    }

    static function deactivation(){
       //the code you want to execute when the plugin deactivates
    }

    static function uninstall(){
      //the code you want to execute when the plugin uninstalled
    }

}

...

// you should use this in your plugin file

register_activation_hook(__FILE__, 'Hooks::activation' );
register_deactivation_hook(__FILE__, 'Hooks::deactivation');
register_uninstall_hook(__FILE__, 'Hooks::uninstall');


This is the simple way I know to do it and the plugins I read the code generally does that way. Hope that helps!

Leave a Comment