Having a PHP function use a global variable as a default?

lilHar

Let's say I have a lot of functions like the following, and will be making more involving some variable. For example, I'll use timezone...

function tell_time($values, $timezone = "Greenwich"){
     // Do something dealing with the timezone
}

function expected_arrival_time($values, $timezone = "Greenwich"){
     // Do something dealing with the timezone
}

function delayed_shipment_arrival($values, $timezone = "Greenwich"){
     // Do something dealing with the timezone
}
// ... and so on....

Now, if the server moves to a different timezone, all of those need to be updated. What would be 'normal' would be something like individually changing all the defaults...

function tell_time($values, $timezone = "Mountain"){
     // Do something dealing with the timezone
}

function expected_arrival_time($values, $timezone = "Mountain"){
     // Do something dealing with the timezone
}

function delayed_shipment_arrival($values, $timezone = "Mountain"){
     // Do something dealing with the timezone
}
// ... and so on....

However, if I'm wanting to make this code accessible in an open source setting, where the server timezone may change frequently depends on who downloads and installs, this may get very cumbersome for many people real fast. However, reworking all the logic isn't ideal. What would be simplest (assuming I have the server variable set), would be something like...

function tell_time($values, $timezone = $_SERVER["timezone"]){
     // Do something dealing with the timezone
}

function expected_arrival_time($values, $timezone = $_SERVER["timezone"]){
     // Do something dealing with the timezone
}

function delayed_shipment_arrival($values, $timezone = $_SERVER["timezone"]){
     // Do something dealing with the timezone
}
// ... and so on....

Then, when someone downloads and installs, it just pulls from a server variable that gets set at install time, or potentially some other global variable. Or maybe pulling from some session data for customer or something else even. The point is, I want default where I can set many functions to share the same default, so that default is easy to update. Is there a good way to do this? I'd prefer not going into every single function and having to change it to setting defaults and pulling from variables with separate internal logic, as it just makes the code filled with busywork for any who come later.

Tim

You could use a constant. Define it at the top of a file and let your functions use that. E.g.

define('CUSTOM_TIMEZONE', 'Mountain');

function tell_time($values, $timezone = CUSTOM_TIMEZONE) {
// Your code here
}

Just change the constants value and it's changed everywhere.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Having a PHP function use a global variable as a default?

From Dev

PHP function returning anonymous function with use / global

From Dev

Use of global variable not working within .each function

From Dev

making sure a function does not use a global variable

From Dev

Use of global variable not working within .each function

From Dev

how to use a global variable inside a function in javascript?

From Dev

Use of Global variable vs Parameter to a function

From Dev

PHP: Set a global variable from within a function

From Dev

How to use global variable in PHP Codeigniter

From Dev

Use global to get a variable from parent function (function in function)

From Dev

How to access to global static variable in static function having variable with same name

From Dev

PHP SET default argument in function as static variable

From Dev

Having trouble getting a PHP class function to use another function in the class

From Dev

Having trouble getting a PHP class function to use another function in the class

From Dev

PHP access global variable in function inside other function

From Dev

Use variable into php public php function

From Dev

Can't use variable outside function even after setting it global

From Dev

The rule of thumb as to when we should use a global variable in a function

From Dev

set global variable AND use stdout from bash function

From Dev

Do I really have to use a global variable for this recursive function?

From Dev

How can I make this javascript function not use a global variable?

From Dev

Why can't I feed a function as a default variable in a PHP function?

From Dev

PHP Can not import a global variable into a function that is inside a class

From Dev

Passing a global variable to a function

From Dev

Django Global function and variable

From Dev

Local variable in global function

From Dev

Using global variable in function

From Dev

Global variable undefined in function

From Dev

Function not updating global variable

Related Related

  1. 1

    Having a PHP function use a global variable as a default?

  2. 2

    PHP function returning anonymous function with use / global

  3. 3

    Use of global variable not working within .each function

  4. 4

    making sure a function does not use a global variable

  5. 5

    Use of global variable not working within .each function

  6. 6

    how to use a global variable inside a function in javascript?

  7. 7

    Use of Global variable vs Parameter to a function

  8. 8

    PHP: Set a global variable from within a function

  9. 9

    How to use global variable in PHP Codeigniter

  10. 10

    Use global to get a variable from parent function (function in function)

  11. 11

    How to access to global static variable in static function having variable with same name

  12. 12

    PHP SET default argument in function as static variable

  13. 13

    Having trouble getting a PHP class function to use another function in the class

  14. 14

    Having trouble getting a PHP class function to use another function in the class

  15. 15

    PHP access global variable in function inside other function

  16. 16

    Use variable into php public php function

  17. 17

    Can't use variable outside function even after setting it global

  18. 18

    The rule of thumb as to when we should use a global variable in a function

  19. 19

    set global variable AND use stdout from bash function

  20. 20

    Do I really have to use a global variable for this recursive function?

  21. 21

    How can I make this javascript function not use a global variable?

  22. 22

    Why can't I feed a function as a default variable in a PHP function?

  23. 23

    PHP Can not import a global variable into a function that is inside a class

  24. 24

    Passing a global variable to a function

  25. 25

    Django Global function and variable

  26. 26

    Local variable in global function

  27. 27

    Using global variable in function

  28. 28

    Global variable undefined in function

  29. 29

    Function not updating global variable

HotTag

Archive