One of the features people tend to love or hate (or simply accept) about writing PHP scripts is how you can mix general constructs of the language – such as conditions – with functions outside of any type of class, namespace, or container.
That is, you can write conditional logic that exists within the global namespace right alongside functions that aren’t part of anything other than the global namespace, as well. This can make for difficult code to maintain.
But the point of this post isn’t to complain – for what it’s worth, I see it as the nature of the language, accept it for what it is, try to avoid it, and work with it whenever something comes across my desk. I’m far less dogmatic about that kind of stuff than I used to be when I first started working as a developer, but I digress.
Anyway, this post is tagged as “WordPress” which doesn’t make a lot of sense, however the purpose of doing so is because I was recently working on an older WordPress-based site that was using Ajax, it wasn’t doing so using the built-in API, and it was basically using Ajax to call a vanilla PHP script.
As such, I thought I’d write a bit about as how it’s still possible to refactor code like this so it’s a little more maintainable even if it’s using a style of coding with which we don’t necessarily like to use.