5

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?

5

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.

  • WordPress will eventually just be an API... So sadly it doesn't have built in things for handling AJAX post submission, or AJAX post modification, so I have to write those things out myself – Nathaniel Schweinberg Feb 19 '14 at 0:10
  • 1
    It sounds like the JSON API plugin might be a good fit for your project in that case? wordpress.org/plugins/json-api – djb Feb 19 '14 at 0:15
  • Eventually, yes. For right now I don't think so because we're not ready to abstract the front end from the back. – Nathaniel Schweinberg Feb 20 '14 at 17:58

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.