如果您使用的是php 7,则请参见以下内容:
此实现将为您提供有关嵌套函数的清晰思路。假设我们在函数foo()中嵌套了三个函数(too(),boo()和zoo())。boo()和zoo()具有相同的命名嵌套函数xoo()。现在,在这段代码中,我清楚地注释了嵌套函数的规则。
function foo(){
echo 'foo() is called'.'<br>';
function too(){
echo 'foo()->too() is called'.'<br>';
}
function boo(){
echo 'foo()->boo() is called'.'<br>';
function xoo(){
echo 'foo()->boo()->xoo() is called'.'<br>';
}
function moo(){
echo 'foo()->boo()->moo() is called'.'<br>';
}
}
function zoo(){
echo 'foo()->zoo() is called'.'<br>';
function xoo(){
echo 'zoo()->xoo() is called'.'<br>';
}
}
}
function test1(){
echo '<b>test1:</b><br>';
foo();
too();
boo();
too();
moo();
xoo();
}
function test2(){
echo '<b>test2:</b><br>';
foo();
too();
zoo();
xoo();
}
现在,如果我们调用test1(),输出将是这样的:
test1:
foo() is called
foo()->too() is called
foo()->boo() is called
foo()->too() is called
foo()->boo()->moo() is called
foo()->boo()->xoo() is called
如果我们调用test2(),输出将是这样的:
test2:
foo() is called
foo()->too() is called
foo()->zoo() is called
zoo()->xoo() is called
但是我们不能同时调用text1()和test2()以避免重新声明错误