I am having a strange issue with getting an array returned from an include. The example taken from the manual shows the behavior I am expecting:
return.php
<?php
$var = 'PHP';
return $var;
?>
noreturn.php
<?php
$var = 'PHP';
?>
testreturns.php
<?php
$foo = include 'return.php';
// This is the expected behavior.
echo $foo; // prints 'PHP'
$bar = include 'noreturn.php';
echo $bar; // prints 1
?>
My usage scnario gives different result. I load Zend_Config by calling it with a simple include that returns an array():
config.php
<?php
/*
* Configuration options loaded in Zend_Config
*/
return array(
'localDB' => array('serverName' => 'TESTDB',
'uid' => 'TESTUSER',
'pwd' => 'TESTPW',
'DB' => 'TESTDB'
),
);
// in the app I can call Zend_config somewhat like this ...
$configfile = 'config.php';
// zend_config takes an array as parameter, returned by the included file...
$config = Zend_config(include $config);
All is fine. Except now I want to overide this array for test configuration, without changing the file, so I do this:
testConfig.php
$testsettings = include_once 'config.php';
// override the array
$testsettings['localDB']['serverName'] = "TEST";
//return the overriden array
return $testsettings;
Now, the weird part. It all works fine when I execute php -f testConfig.php and var_dump() $testsettings.
But if I include this file in a testcase to have orverriden settings value, the result is always a (bool) true, like the example include shown at top with no return value set .
I have thought of a few workarounds for this, but was wondering out of curiosity if anyone had a clue as to why it does this.
include_once
returns true
every time after the first one. So the line
$testsettings = include_once 'config.php';
sets $testsettings
to true if you've included config.php
anywhere in earlier code.
Perhaps it is not a bool, but a count of the array.
It's in the docs. Check out the section on Handling Returns - http://us2.php.net/manual/en/function.include.php . But basically - don't do this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.