OOP and WordPress Plugin Development – Instantiating Objects

I am confusing with OOP and WordPress. It would be nice to get some advice.

I have this files:

some-plugin.php (WordPress plugin main file.)

includes/class-autoloader.php (A custom autoloader that follows WordPress standards)

includes/class-plugin.php (It supposed to have everything i need for the core like versions, paths etc)

includes/class-frontend-scripts.php (It extends a Scripts class and implements a Hook interface)

includes/class-backend-scripts.php (It extends a Scripts class and implements a Hook interface)

In the some-plugin.php file i do:

$autoloader = new Autoloder();
$autoloader->register();

// so here i can use any of those classes without require them manually.

$plugin = Plugin::getInstance();

$frontend_scripts = new FrontendScripts( ‘app’, ‘test’ ); // list of script names that should be registered
$frontend_scripts->hooks(); // Enqueue all the scripts passed as arguments.

$backend_scripts = new BackendScripts( ‘admin’ ); // the same as for frontend
$backend_scripts->hooks(); // It does the same as Frontend_Script as the hooks method comes from their parent class.
$backend_scripts->register(‘admin’); // Enqueue only a single script an example.

What is the best practice of instantiating those FrontendScripts and BackendScripts objects?

Should i pass them to the main plugin class as args?

Should i just make instances inside the Plugin class without any arguments passed to the main class?

Read more here:: OOP and WordPress Plugin Development – Instantiating Objects

Leave a Reply

Your email address will not be published. Required fields are marked *