I'm working in the Wordpress environment which itself heavily relies on globals and the codebase I'm maintaining introduces some more. I want this to change and so I'm trying to think how should I handle this.

For the globals our code has introduced I think I will set them as dependencies in the constructor or in getter / setter so that I don't rely on them being globals and then refactor the old codebase little by little so that we have no globals.

With Wordpress globals I was thinking to wrap all WP globals inside a Wrapper class and hide them in there. Like this

class WpGlobals {
    public static function getDb() {
        global $wpdb;
        return $wpdb;

Would this be of any help? The idea is that I centralize all globals in one class and do not scatter them through the code, so that if Wordpress kills one of them I need to modify code only in one place.

What would you do?


I recently started working on a Wordpress project. We wrap all calls to Wordpress functions into a wrapper class. This makes it easy to unit test our code.

I have a function that returns the db connection like you do. This way I can mock it.

| improve this answer | |

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.