Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.