有人問python函數能夠返回函數,php如何實現同樣的功能php
python 中函數是第一類對象,函數名字是函數對象的引用,函數名能夠賦值給變量,能夠做爲參數傳遞給函數,能夠做爲函數的返回值從函數中返回。python
#! /usr/bin/python #coding:utf-8 """ 參加黃哥python遠程視頻培訓,幫你完成從不會寫代碼到會寫代碼解決問題的過渡。 python遠程視頻培訓 https://github.com/pythonpeixun/article/blob/master/index.md python北京週末培訓班 https://github.com/pythonpeixun/article/blob/master/beijing_weekend.md 諮詢:qq:1465376564 企業內訓,我的培訓,請諮詢黃哥電話:18610508486 """ def f1(f2): def f3(): return f2 return f3 def f2(): print("I come from f2") foo = f1(f2) # 返回值函數 print(foo()) # foo()的值仍是函數 #<function f2 at 0x10c0ab140> foo()() # 帶扣號調用 #I come from f2
有人問php如何實現上面的功能呢?git
php匿名函數(Anonymous functions),也叫閉包函數(closures),容許 臨時建立一個沒有指定名稱的函數。最常常用做回調函數(callback)參數的值。github
<?php $f2 = function(){ return "I come from f2()"; }; function f1($func) { $f3 = function() use($func){ return $func(); }; return $f3; } print f1($f2)(); print "\n";