0

I'm trying to wrap my head around using Wordpress the OOP way, and I'm a bit confused about how to properly instantiate a series of classes I've created for my theme. I've read tons of questions and tutorials but I'm still a bit confused, so I hope someone can help.

From what I've read it seems that one common practice is to create a singleton class that instantiates all other classes, and that should look something like this:

namespace sample; 

class Init {

    private static $instance = null;

    public static function instance() {

        if( self::$instance === null ) {
            self::$instance = new Init();
        }

        return self::$instance;

    }

    public function init() { 
        // Code goes here
    }       

}

The Init.php file is in a folder called classes that is in the root of my theme, and I load the Init class from functions.php (I'm using Composer):

// Include Composer's autoload
require_once( __DIR__ . '/vendor/autoload.php' );

// Initialize all classes
$Init = new \sample\Init;
$Init->init(); 

Now, I have a form that users can fill to register on the site. Registration is handled by the WPGraphql plugin, but I want to do some custom validation so I want to hook into an action provided by the plugin and load a ValidateUser class. Which of the following two is correct?

Option 1 — Load ValidateUser in Init and hook into the action in the ValidateUser constructor:

class Init {

    private static $instance = null;

    public static function instance() {

        if( self::$instance === null ) {
            self::$instance = new Init();
        }

        return self::$instance;

    }

    public function init() { 
        // Load the class straight away
        $ValidateUser = new ValidateUser(); 
    }       

}

and

class ValidateUser {

    public static function __construct() {
        add_action( "graphql_before_resolve_field", array( $this, "validate_new_user" ), 10, 7 );
    }

    public function validate_new_user() { 
       // Validation code goes here
    }       

}

Option 2 — Hook into the action already in Init and load ValidateUser upon hooking:

class Init {

    private static $instance = null;

    public static function instance() {

        if( self::$instance === null ) {
            self::$instance = new Init();
        }

        return self::$instance;

    }

    public function init() { 
         add_action( "graphql_before_resolve_field", array( $this, "on_validation" ), 10, 7 );
    }       

    public function on_validation() { 
        // Load the class upon hooking
        $ValidateUser = new ValidateUser(); 
    }

}

and

class ValidateUser {

    public static function __construct() {
        $this->validate_new_user(); 
    }

    public function validate_new_user() { 
       // Validation code goes here
    }       

}

Option #2 looks better to me but I'm not sure. Is there one way to prefer over the other, or is there actually a third way?

1

Your Answer

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

Browse other questions tagged or ask your own question.