Not sure why I can't modify an array by calling a function that includes a file that then references that array.
Here is the situation:
config.php:
$db['default']['password'] = "password";
$db['default']['database'] = "dbname";
check_if_local_file_exists(__FILE__);
function check_if_local_file_exists( $calling_file )
{
$local_version = dirname( $calling_file ) . '/' . basename( $calling_file , '.php' ) . '.local';
if ( file_exists ( $local_version ) ) {
@include_once( $local_version );
}
} // function
config.local:
global $db;
$db['default']['password'] = "password_modififed";
$db['default']['database'] = "dbname_modified";
Not sure why I can't modify the $db array in the included file: config.local that is included by the check_if_local_file_exists function.
When I print_r ($db ) after calling check_if_local_file_exists(FILE) in config.php, the 开发者_开发百科$db array has not been modified at all. . .
You have to include the file at load. Php isn't reparsed after initial load. Try not using the check function and just include the file directly as a test and see if that works for you. Also, try
return @include(....);
instead of just
@include(....);
You should not use the @ in front of the include call. It could hide important debugging information.
Is the $db variable in config.php created in the global scope? That is the lines you've shown us are not within a function or within a file that is included by a function?
Have you verified that the file is being include? This can be as simple as echoing "I've been included!" in the config.local
Additionally if the file has been included, try var_dump($db);
after the global declaration but before you modify it, and ensure you are accessing the right variable (this will answer the question about scope as well).
I suspect the file may not be being included and you are not being warned because of the @ symbol, or the fact that it does nothing if the file doesn't exist.
In all your conditions, I do not see a single debug statement such as,
else echo 'file does not exist';
How do you know if the file even exists where you say it does? When using include(....) do not make all those checks and then use the superessor operator. That's so much time and resources wasted. Use one. Most likely, the condition checks since the suppression is way slower and add some debug info there.
If it is a config file, why make it optional? I'd rather use an include. Configurations are essential.
Moreover, the values in the config local are the same in your example. There shouldn't be any modification.
So in your test, are the values different? If yes, it should work. If you are modifying the array outside a function, try removing the global declaration. Try a simple echo in the included file to see if it executes. Lastly, rename the file with a .php extension and try again.
Try wrapping your code in php tags inside the include file. If you know which variable you are modifying before hand, being the global statement before you include the file. That should solve it.
精彩评论