Single Responsibility Principle and WP Classes

Still working my way through OOP and Solid principles. I’m having a hard time getting around having to have many new SomeClass(); calls, or adding them to an action hook ( add_action( 'init', array( 'SomeClass', 'get_instance' ) ). Case in point: I have a posts controller I’m using to handle anything having to do with posts: creation, deletion, modifying its status via ajax, etc. I would think SRP would call for different classes for post creation/modification, ajax requests, etc. Is there a better way to instantiate these classes other than new / add_action? I feel like there will be an excessive number of classes and class instantiations and someone would have come up with something by now. Am I crazy?

Related posts

1 comment

  1. WordPress itself is not very object oriented – it has objects (like WP_User, for example), but most interactions with the core API take place through plain function calls. It also depends widely on global state, though it does a good job of managing this through the abstractions of actions and filters.

    In my experience there is a sweet spot between sticking to vanilla WP function calls and building classes. Go too far towards OO and you’ll end up reimplementing things that are already quite convenient. This is almost certainly the road you’re heading down if you are instantiating a new class for post creation.

    But if you’re really doing something else – an API interface, a complicated accounts system, payments etc – then your points of contact with WP will be minimal and you can be as SOLID as you like within the realm of the problem you’re dealing with.

Comments are closed.