有没有办法$foo从内部访问inner()?
function outer()
{
$foo = "...";
function inner()
{
// print $foo
}
inner();
}
outer();
Run Code Online (Sandbox Code Playgroud)
sim*_*aun 43
PHP <5.3不支持闭包,因此您必须将$ foo传递给inner()或者从outer()和inner()(BAD)中创建$ foo全局.
在PHP 5.3中,您可以这样做
function outer()
{
$foo = "...";
$inner = function() use ($foo)
{
print $foo;
};
$inner();
}
outer();
outer();