开发者

Global vs. Define

开发者 https://www.devze.com 2023-03-26 06:46 出处:网络
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 (creatin

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 whic开发者_开发技巧h way is best practice?


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


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.


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)
}


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


Both work fine as long as you remember to choose a distinctive name (all-uppercase for defines) 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消