1

I'm looking to do this:

/* example filename: config_load.php */

$config_file = "c:\path\to\file.php";

function read_config($file = &$config_file)
{
$settings = array();
$doc = new DOMDocument('1.0');
$doc->load($file); 
$xpath = new DOMXPath($doc); 
$all=$xpath->query('appSettings/add');
foreach ($all as $setting) {$settings[$setting->getAttribute('key')]=$setting->getAttribute('value');}

return $settings;
}

/* end config_load.php */

So then when I actually call the file, it'd go like this -

require_once "config_load.php";
// $config_file = "c:\path\to\file2.php"; //could also do this
$config = read_config();

This way if I don't specify a file, it's going to read the default config file. I could also define $config_file anywhere before I make the function call. And someone without access to the config_load file need not worry about being able to load a different file, they can define it anywhere before they make the read_config() call.

1

2 Answers 2

0

It's not possible:

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

~ http://www.php.net/manual/en/functions.arguments.php#functions.arguments.default

However, you can get around it like this:

function read_config($file = false) {
    global $config_file;
    if ($file === false) $file = $config_file;

    $settings = array();
    $doc = new DOMDocument('1.0');
    $doc->load($file); 
    $xpath = new DOMXPath($doc); 
    $all=$xpath->query('appSettings/add');
    foreach ($all as $setting) {$settings[$setting->getAttribute('key')]=$setting->getAttribute('value');}

    return $settings;
}

or like this:

function read_config($file = false, $config_file = false) {
    if ($file === false && $config_file !== false) $file = $config_file;

    $settings = array();
    $doc = new DOMDocument('1.0');
    $doc->load($file); 
    $xpath = new DOMXPath($doc); 
    $all=$xpath->query('appSettings/add');
    foreach ($all as $setting) {$settings[$setting->getAttribute('key')]=$setting->getAttribute('value');}

    return $settings;
}
Sign up to request clarification or add additional context in comments.

3 Comments

So I've got to use a global - that's the only way to do this?
Added explanation + other option
Any idea why I can't get this to work with a constant? I'm trying define("CONFIG_FILE", "c:\path\file.php"); function read_config($file = CONFIG_FILE){code}; and calling with read_config(), and no luck. Constant seems like the better option here.
-1

Yes you can:

<?php

$greet = function()
{
   return "Hello";
};

$a = $greet();
echo $a;
?>

Read more here: http://php.net/manual/en/functions.anonymous.php

1 Comment

And how would I possibly use this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.