我希望能够调用一个函数,它将在调用函数中设置一个或多个局部变量。例如:
function someFunc () {
loadTranslatedStrings($LOCALS, "spanish");
echo $hello; // prints "hola";
}
function loadTranslatedStrings (&$callerLocals, $lang) {
if ($lang == 'spanish')
$callerLocals['hello'] = 'hola';
else if ($lang == 'french')
$callerLocals['hello'] = 'bonjour';
else
$callerLocals['hello'] = 'hello';
}
(我猜这是不可能的,但不妨问......)
答案 0 :(得分:5)
你可以这样做......
function someFunc () {
loadTranslatedStrings($lang, "spanish");
extract($lang);
echo $hello; // prints "hola";
}
答案 1 :(得分:4)
我认为你最接近的是使用extract:
function someFunc()
{
extract(loadStrings('french'));
echo $hello;
}
function loadStrings($lang)
{
switch($lang)
{
case 'spanish':
return array('hello' => 'hola');
case 'french':
return array('hello' => 'bonjour');
}
}
答案 2 :(得分:0)
您可以使用$GLOBALS
:$GLOBALS['hello'] = 'hola';