PHP best practices: repass variables from config file when calling functions or use global?
Asked Answered
S

7

7

I have a program that I use on several sites. It uses require('config.php'); to set any site dependant variables like mysql connect info, paths, etc.

Let's say that I use one of these site-dependant variables in a function, like $backup_path.

This variable was initially declared in config.php, and does not appear in the main program file.

I need to access this variable in function makebackup($table_name); (also in a separate functions.php file).

Is it better to say

makebackup('my_table');

and then use "global $backup_path" inside the function, or is it better to call the function using

makebackup('my_table',$backup_path);

The argument for the first is that it keeps the main program flow simple and easy to understand, without clutter.

The argument for the second is that it might not be obvious that the variable $backup_path exists after some time has passed, and debugging or reworking could be difficult.

Is one or the other of these techniques "standard" among professional programmers? Or should I be using $_SESSION to declare these global variables?

Shalloon answered 18/9, 2009 at 9:16 Comment(0)
D
1

Remember that you can set a default value for your function:

function makebackup($table, $dir = CONFIG_BACKUP_PATH)

That way you won't have to supply the variable in the default case, you can simply assume that the configured backup path is the default.

This assumes that you are using constants, not global variables.

Drusilladrusus answered 18/9, 2009 at 9:58 Comment(0)
E
5

The second alternative,

makebackup('my_table', $backup_path);

is a reusable function and therefore generally preferable. The extra argument is not a big price for reusability.

If you are entirely sure that you'll ever use that function in that particular application only, and for $backup_path only, then maybe consider the global alternative. Even then it's good to check that the global variable actually exists. And be aware that it's extremely difficult to get rid of globals once you start using them.

Encumber answered 18/9, 2009 at 9:29 Comment(1)
I like this option because right up front, you can see that the global variable is being used.Pileup
S
1

I'm think you must use Singleton of Factory class config for this purposes.

function makebackup($table)
{
   $backup_path = ConfigFactory().getConfig($some_site_specific_data).getBackupPath()
   mysqldump($table, $backup_path)
}
Schulman answered 18/9, 2009 at 9:23 Comment(3)
Can you provide a link so I can find out more?Shalloon
I do like the idea of using a getBackupPath() function to find this info. That way it's more modular and you can change the internals of getBackupPath() later if you need to.Pileup
@Anrew Singleton and Factory - most popular design patterns (en.wikipedia.org/wiki/Design_pattern_%28computer_science%29) Singleton - en.wikipedia.org/wiki/Singleton_pattern#PHP Factory - en.wikipedia.org/wiki/Factory_pattern#PHPSchulman
Y
1

Passing references around is far easier to test (you can give mock configuration objects). Globals less so. You can assert that the reference is not null on the method. I would call testability best practice.

Yonit answered 18/9, 2009 at 9:29 Comment(0)
D
1

Remember that you can set a default value for your function:

function makebackup($table, $dir = CONFIG_BACKUP_PATH)

That way you won't have to supply the variable in the default case, you can simply assume that the configured backup path is the default.

This assumes that you are using constants, not global variables.

Drusilladrusus answered 18/9, 2009 at 9:58 Comment(0)
P
1

Label that global variable

Personally, I have also taken to marking global variables very clearly. If I must use them, I want to be clear about them.

So here, I'd rename $backup_path to $GLOBAL_backup_path. Every time I saw it, I'd know to be careful with it.

Pileup answered 18/9, 2009 at 10:1 Comment(0)
S
1

An alternative option is using php constants with define().

Your config.php will set constants for every parameter (mysql connection, css style, wathever). Then you will not need to pass variables to functions nor using global.

One downside is that you can define only booleans, floats, strings or integers, no complex data structures.

Sweatt answered 18/9, 2009 at 10:40 Comment(0)
A
0

Not sure there's really a 'right' way to do it, but another option would be something like this:

function makebackup($table, $backup_path = '') {
    if ( $backup_path == '' ) {
        if ( isset($GLOBALS['backup_path']) ) {
            $backup_path = $GLOBALS['backup_path'];
        }
        else {
            die('No backup path provided');
        }
    }
 }

That way you can either pass in the value (for testing and future use) or if you don't pass it in, then the function will look for a possible global variable.

Albertinaalbertine answered 18/9, 2009 at 10:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.