Home  >  Article  >  Backend Development  >  关于php闭包附加状态的问题

关于php闭包附加状态的问题

WBOY
WBOYOriginal
2016-06-06 20:10:50978browse

直接上代码:

<code><?php function enclosePerson($name){
        return function($doCommand) use ($name) {
                return sprintf('%s, %s', $name, $doCommand);
        }
}
$func = enclosePerson("Simon");

echo $func("you are a cool man!");
?>
</code>

理论应该打印:Simon, you are a cool man!

但实际打印为空!

什么问题造成的呢?

php version:5.5.29

回复内容:

直接上代码:

<code><?php function enclosePerson($name){
        return function($doCommand) use ($name) {
                return sprintf('%s, %s', $name, $doCommand);
        }
}
$func = enclosePerson("Simon");

echo $func("you are a cool man!");
?>
</code>

理论应该打印:Simon, you are a cool man!

但实际打印为空!

什么问题造成的呢?

php version:5.5.29

因为你语法错误
return 后面少了个 ;

<code>return function($doCommand) use ($name) {
                return sprintf('%s, %s', $name, $doCommand);
        };</code>

晕,LZ是在web页面中写的吧? 一片空白这种问题很明显是出错了。而且是致命性的错误。看看错误日志就知道了。

从我这边看到的错误是:

<code>PHP Parse error:  syntax error, unexpected '}' in - on line 6</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn