5

My question is that when we hook to a certain action using the oop way for example

class My_Plugin_Class { 
    public function __construct( ) {
       add_action( 'admin_init', array( &$this, 'some_function' ) );
    }

    public function some_function() {
       //do something here
       include( 'some_php_file.php' );
    }
}

Then inside that some_php_file.php you included

class Some_Class_Again { 
    public function __construct( ) {
       add_action( 'init', array( &$this, 'another_function' ) );
    }

    public function another_function() {
       //do something here like enqueue script
    }
}

is this possible to do? that you hooked a method in your class in an action hook and then inside that method you included another class file making or hooking into another action hook. because when I tried it nothing's happening in the second class. I just want to know if this is possible, if it is am I doing something wrong? or if it's not please tell me why. I have this in mind for quite some time now.

  • Yes, the rule being that the second hook must not have already fired. In your example, that will have happened, init fires before admin_init. – Andrew Bartel Dec 1 '14 at 3:48
  • oh nice thanks! @AndrewBartel . Appreciate the answer, So then I must first know what hook will be fired first, right? then I can do it? How about if I set the priority variable for the add_action would that make a difference? because this is the first time I actually used this way of hooking. – GreyWolfram Dec 1 '14 at 4:56
  • The priorities determine what order things are fired in that action only, so it wouldn't let you hook on to an action that had already been fired. – Andrew Bartel Dec 1 '14 at 5:06
  • Oh ok the whole time I was thinking it would determine which fires first. So I put on that priority variable. Nice will do what you told me and research on who gets trigger first. :D – GreyWolfram Dec 1 '14 at 5:10
  • Cool, when you have working code, you might want to post it as an answer and accept to help future visitors. Happy coding. – Andrew Bartel Dec 1 '14 at 5:30
1

When it comes to the oop way of doing things you need more than just the class, you also need to instantiate it as an object at some point if you want the action to fire.

class myClass{

    function __construct(){
        add_action( 'init', array( $this, 'someFun' ) );
    }

    function someFun(){
        include( 'my-script.php' );
    }

}
//instantiate an instance of myClass
new myClass();

followed by, in my-script.php,

class anotherClass{

    function __construct(){
        add_action( 'wp', array( $this, 'moreFun' ) );
    }

    function moreFun(){
        //do something.
    }

}

won't do anything more than include the definition of anotherClass. It's moreFun function won't be called; nothing else will happen.

To get it to do moreFun at wp you must also instantiate anotherClass, e.g.

    function someFun(){
        include( 'my-script.php' );
        new anotherClass();
    }

However, what's the point in this? Why include my-script.php in someFun when you could have included it initially and not risked missing it somehow elsewhere?

The only reason I can think for doing this is if you want to have more than one definition of anotherClass (something that is quite poor practice in general).

It is better to do this:

class pluginRootClass{

    function __construct(){
        //include all your class definitions
        include( 'my-script.php' );
        include( 'scripty.php' )

        //then do your actions
        add_action( 'init', array( $this, 'someFun' ) );
    }

    function someFun(){
        new anotherClass();
    }

}

//instantiate an instance of pluginRootClass
new pluginRootClass();

This works a lot better because there's no chance of accidentally doing a $x = new anotherClass(); when anotherClass isn't defined because the action 'init' (or whichever action it is) hasn't fired yet and so breaking everything.

In addition, for the pluginRootClass there is the risk of it being instantiated more than once. Thus calling the __construct function more than once, thus including the same class definitions more than once, thus causing an error. Hence it is better to stop this from happening by making a singleton of pluginRootClass.

function myPlugin(){
    //If object already exists return it, if not create it, save it and return it
    if( ! ( $ob = wp_cache_get( 'root', 'plugin-namespace' ) ) ){
        $ob = new pluginRootClass();
        wp_cache_set( 'root', $ob, 'plugin-namespace' );
    }
    return $ob;
}
myPlugin();

and then only calling the class by using the function myPlugin.

  • actually I was creating it for somehow making a structure where I would only just add a folder or a php file that would run when My Class is instantiated. and I'm new in OOP. Thanks. – GreyWolfram Dec 10 '14 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.