register_activation_hook with include file

I’m yanking my hair out with the following code, please help.
I have 3 files.

File1.php:

Read More
Class File1 {
    public function file1_register() {
        //register some short codes;
    }
    public function file1_unregister() {
        //unregister previous short codes;
    }
}

File2.php:

Class File2 {
    public function file2_register() {
        //create some database tables.
    }
    public function file1_unregister() {
        //delete previous tables.
    }
}

MyPlugin.php:

function MyActivation() {
    include_once (dirname(FILE).'/file1.php');
    $File1 = new File1;
    $File1::file1_register();
}
register_activation_hook(FILE, 'MyActivation');

The function file1_register just refuse to work, eventually I would like to run file2_register as well, but at this point, I can’t see the error or problem with my code, please help.

Related posts

Leave a Reply

1 comment

  1. You have two options either define you register methods as static and then you can avoid instantiating your classes or even calling the MyActivation function ex:

    File1.php:

    Class File1 {
        static function file1_register() {
            //register some short codes;
        }
        static function file1_unregister() {
            //unregister previous short codes;
        }
    }
    

    File2.php:

    Class File2 {
        static function file2_register() {
            //create some database tables.
        }
        static function file1_unregister() {
            //delete previous tables.
        }
    }
    

    MyPlugin.php:

    include_once dirname( __FILE__ ).'/File1.php';
    register_activation_hook( __FILE__, array( 'File1', 'file1_register' ) );
    include_once dirname( __FILE__ ).'/File2.php';
    register_activation_hook( __FILE__, array( 'File2', 'file2_register' ) );
    

    OR simply change the “Paamayim Nekudotayim” (::) to the arrow operator (->) in your current MyActivation function which means replace this line:

    $File1::file1_register();
    

    with this:

    $File1->file1_register();