Javascript 嵌套函数,PHP中的第一类函数
对于Python、Js和PHP中的函数,它们都说它们是第一类对象。我想确保我的PHP示例是正确的。是否有其他方法将此代码移植到PHP Python 3:Javascript 嵌套函数,PHP中的第一类函数,javascript,php,python-3.x,Javascript,Php,Python 3.x,对于Python、Js和PHP中的函数,它们都说它们是第一类对象。我想确保我的PHP示例是正确的。是否有其他方法将此代码移植到PHP Python 3: def a(): print("init") def b(): print("inside") def c(): print("inception") return c return b a()()() function a() { console.
def a():
print("init")
def b():
print("inside")
def c():
print("inception")
return c
return b
a()()()
function a() {
console.log("init")
function b()
{
console.log("inside")
function c()
{
console.log("inception")
}
return c
}
return b
}
s = a()()()
function a()
{
echo "init".PHP_EOL;
$x = function()
{
echo "inside".PHP_EOL;
$y = function()
{
echo "inception".PHP_EOL;
};
echo "b".PHP_EOL;
return $y;
};
return $x;
}
a()()();
结果
Javascript(节点):
def a():
print("init")
def b():
print("inside")
def c():
print("inception")
return c
return b
a()()()
function a() {
console.log("init")
function b()
{
console.log("inside")
function c()
{
console.log("inception")
}
return c
}
return b
}
s = a()()()
function a()
{
echo "init".PHP_EOL;
$x = function()
{
echo "inside".PHP_EOL;
$y = function()
{
echo "inception".PHP_EOL;
};
echo "b".PHP_EOL;
return $y;
};
return $x;
}
a()()();
结果
PHP5.5:
def a():
print("init")
def b():
print("inside")
def c():
print("inception")
return c
return b
a()()()
function a() {
console.log("init")
function b()
{
console.log("inside")
function c()
{
console.log("inception")
}
return c
}
return b
}
s = a()()()
function a()
{
echo "init".PHP_EOL;
$x = function()
{
echo "inside".PHP_EOL;
$y = function()
{
echo "inception".PHP_EOL;
};
echo "b".PHP_EOL;
return $y;
};
return $x;
}
a()()();
结果
让我们在PHP中重试
$b = a();
$c = $b();
$c();
结果
这是PHP中调用嵌套函数的唯一方法吗
更新后续问题,在PHP中,能否以与Python或JS中相同的方式返回函数?或者必须首先将其作为闭包分配给变量 您的示例将在PHP7+中运行。在此之前,您需要使用临时变量或
调用\u user\u func()
(但这将非常难看)
你能分享PHP 7+兼容性的文档链接吗?我在文档中找不到这个链接有什么原因吗?不知道。我在使用此功能的文档中找到了此示例,但没有提到它在7.0之前不起作用:找不到任何其他内容(除了一些迁移指南)。