0

In instance where I find myself wanting to use a global variable, I often give myself permission to use a static function instead. So instead of

global $verbose;
if ( $verbose ) { ... }

I do

function verbose($status = NULL) {
  static $current_status;
  if ( is_bool($status) ) {
    $current_status = $status;
  }
  return $current_status;
}

This basically accomplishes the same thing; is it any better or as bad as using global variables?

user151841
  • 17,377
  • 29
  • 109
  • 171
  • you have my permission to use static or global, or dance naked and howl at the moon(death star) –  Jul 28 '15 at 03:13
  • 1
    I think they are both bad. Code gets tied and harder to test when you're using either one. Take a look at this [question](http://stackoverflow.com/questions/9201511/global-vs-function-vs-static-class-method) and this [blog post](https://www.phparch.com/2010/03/static-methods-vs-singletons-choose-neither/). – Marcos Dimitrio Jul 28 '15 at 03:13
  • conceptually you are doing the same thing only you are adding function call overhead to the mix – Orangepill Jul 28 '15 at 04:10

0 Answers0