0

I'm using Composer PSR-4 to autoload classes, but when I try to call register_activation_hook using a classname if get the following error (same for register_deactivation_hook):


call_user_func_array() expects parameter 1 to be a valid callback, 
class `Activation` not found

The Autoload portion of composer.json looks like so...


"autoload": {
    "classmap": [],
    "psr-4": {
        "BlackOpz\\OOPBookPlugin\\Inc\\": "./src/inc"
    },
    "files": [
        "src/support/exceptions.php"
    ]
},

...which adds the following line to my vendor/composer/autoload_psr4.php file...


    'BlackOpz\\OOPBookPlugin\\Inc\\' => array($baseDir . '/src/inc'),

My directory structure is like so...


oop-book-plugin
 - assets
   - composer (directory)
   - filp (directory)
   - kint-php (directory)
   - psr (directory)
   - autoload.php
 - src
   - css
   - inc
     - Activation.php
     - Deactivation.php
 - etc
     - ...

My Activation.php looks like so...


/**
 *
 * @package     BlackOpz\OOPBookPlugin
 * @wordpress-plugin
 */
namespace BlackOpz\OOPBookPlugin\Inc;

class Activation {

    /**
     * Activate Plugin.
     *
     * @return void
     */
    public static function activate() {
        flush_rewrite_rules();
    }
};

My Deactivation.php looks like so...


/**
 *
 * @package     BlackOpz\OOPBookPlugin
 * @wordpress-plugin
 */
namespace BlackOpz\OOPBookPlugin\Inc;

class Deactivation {

    /**
     * Deactivate Plugin.
     *
     * @return void
     */
    public static function deactivate() {
        flush_rewrite_rules();
    }
};

My main class which looks like so...


// Load Composer Autoload.
if ( file_exists( dirname( __FILE__ ) . '/assets/vendor/autoload.php' ) ) {
    require_once dirname( __FILE__ ) . '/assets/vendor/autoload.php';
}

use BlackOpz\OOPBookPlugin\Inc\Activation;
use BlackOpz\OOPBookPlugin\Inc\Deactivation;

// Instantiate the class.
if ( ! class_exists( 'Book' ) ) {

    /**
     * OOP Book Plugin Class
     */
    class Book {

        // This only works INSIDE the class with an instantiated class (below)
        public function activate() {
            Activation::activate();
        }

        // This only works INSIDE the class with an instantiated class (below)
        public function deactivate() {
            Deactivation::deactivate();
        }
    }

    // The instantiated class.
    $book = new Book();
    $book->register();


    // THIS WORKS
    // register_activation_hook( __FILE__, array( $book, 'activate' ) );
    // register_deactivation_hook( __FILE__, array( $book, 'deactivate' ) );

    // THIS DOES NOT WORK
    register_activation_hook( __FILE__, array( 'Activation', 'activate' ) );
    register_deactivation_hook( __FILE__, array( 'Deactivation', 'deactivate' ) );

}
2
  • 1
    You need to use the full classname including namespace: array('BlackOpz\OOPBookPlugin\Inc\Activation', 'activate') (use does not help here, because you're not loading the class here, but WordPress does so at some other point, where no use for that class is present)
    – kero
    Aug 26 '18 at 20:03
  • 1
    Thank you for your quick response AND the link (it explains it well).
    – sleeper
    Aug 26 '18 at 20:29