简体   繁体   中英

Global vs. Define

I have a php file in which I need to be able to easily flip a switch. The switch is set to 1 for local server and 0 for production server. local_on='1'; or local_on='0';. Which way is better (creating a global or using define)? If either way is good which way is best practice?

A global variable is, as its name indicates, variable -- which means its value can be changed by any portion of your code ; which, in your case, is probably not what you want.

On the other hand, a constant (that you'll set with define ) is... well... constant ; and, as such, cannot be modified once set ; which is probably what you want, in your case.


Considering the variable vs constant idea, for this kind of switches, I generally use define() and constants.

define . You can't change its value later, and its value is always available in all scopes, tersely.

The define is the better choice of the two, because global variables are bad news for reasons I'm sure you're already familiar with, and because you have to remember to declare global $var in every function and method in your code. Defined constants are automatically available everywhere. Additionally, a variable could inadvertently be set from one state to the other during the running of your script. This could cause some really hard-to-find bugs if it happened.

Another way that performs a little better than define symbols and minimizes name clashes is a class declaration like

abstract config {
    const LOCAL = true; // toggle to false
// or maybe
    const SERVER = 'local'; // toggle to 'remote'
// (maybe having if (config.SERVER == 'remote') would be more readable in some
// cases than if (!config.LOCAL) depends on your app)
}

Both work fine as long as you remember to choose a distinctive name (all-uppercase for define s) and use the modern way to access global variables via the $GLOBALS superglobal (sic). Also, global variables are, well, variable , so you could, in theory, change its value by accident or so.

To simplify deployment and not setting or unsetting the switch by accident, I'd recommend automatically setting it automatically by examining the properties of $_SERVER , like

// Turn on debugging code on local machine
define('MYPAGE_LOCAL_ON', $_SERVER['SERVER_NAME'] == 'my-dev-box');

Also, I don't see why you'd set the switch to a string or an integer for that matter. The boolean values true and false seem more appropriate.

I prefer define . Reasons:

  • You can't redefine it accidentally
  • You can use in other scope (ex. functions) without ugly $GLOBALS construction

PS: since PHP 5.3 you can use const , not only define to declare constants. It's more readable for me

In short terms, define is what you look for ( for said reasons ).

However, come the future development, you might look for something like a dependency for your whole application providing the context it is running in. This can not be done with constants, so then define but as well as global variables are both wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM