I am a bit confused about using classes in sage 10. The last project I did was very complex, in a sense that I had to use lots of hooks to customise the functionality of woocommerce, so I ended up with a woocommerce.php file with over 1000 lines, in the app folder. My next project will be even more complex, and I don’t want to create a plugin or write everything in a single file, I would like to use classes, as in a plugin. For example, a class in my Controllers folder would look like this :
<?php
namespace app\Controllers;
class WooController
{
public function __construct()
{
add_action('init', [$this, 'register_roles']);
add_action('wc_hook_1', [$this, 'something_1']);
add_action('wc_hook_2', [$this, 'something_2']);
add_action('wc_hook_3', [$this, 'something_3']);
// ...
}
public function register_roles()
{
// ...
}
}
I would use this class to contain every hook that is woocommerce related. In a separate Utilities folder, I’d create a WooUtility class with static methods to keep my class cleaner. However for this to work, I need to create an instance of the WooController class. For now, inspired from another post, I added this function, but it feels wrong to do it this way:
function load_controllers($ns = 'App\Controllers') {
foreach(glob(__dir__ . '/Controllers/*.php') as $fn) {
$class = $ns . '\\' . basename($fn, '.php');
new $class();
}
}
load_controllers();
I was going to use Service Providers, but I asked a Laravel dev, and they told me I shouldn’t use a Service Provider for this, so now I’m a bit confused about the role of Service Providers in wordpress.
Where should I add the hooks? How should I structure the theme if I don’t want to write a separate plugin?