Re: [請益] callback與匿名函式

看板PHP作者 (銀色)時間12年前 (2013/09/22 17:19), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《edash (團長)》之銘言: : Some PHP functions use a function you provide them with to do part of their : work. For example, the usort() function uses a function you create and pass to it : as a parameter to determine the sort order of the items in an array. : Although you can define a function for such purposes, as shown previously, : these functions tend to be localized and temporary. To reflect the transient : nature of the callback, create and use an anonymous function (also known as a : closure). : 節錄書上兩段對匿名函式(anonymous function)的介紹,第一段提及某些函式可能會利 : 用其他函式完成運作,並舉usort()為例子。 : 第二段則是說明前一段的情境其實也可以自行定義函式解決,只是函式卻有可見範圍與內 : 部變數生命週期(不太確定自己解讀localized and temporary是否完全貼切?)的限制。 these functions tend to be localized and temporary 這句應該只是單純說這種函式傾向於局部性 / 暫時性會使用到的。 To reflect the transient nature of the callback, create and use … 應對著這種暫時性的需求,你可以採用匿名函式來處理(也就是所謂的 closure)。 : 最後一句「To reflect...anonymous function」,我推測應該是因為前面提到函式有 : 「localized and temporary」的特性,才提到匿名函式可以因應callback的特性。 : 反覆思考後,還是想不太出來「transient nature」是什麼意思,transient在字典裡是 : 「短暫的; 一時的; 瞬間的」等意思,感覺都搭不太起來。希望有板友能解惑 : 關於個人解讀的部分,如有錯誤,也請板友多多指教。謝謝 -- 嗯,其實我也通篇翻錯?(驚 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 1.34.144.68

09/23 15:33, , 1F
謝謝回應,我再多想想。
09/23 15:33, 1F
文章代碼(AID): #1IFhOkv1 (PHP)
討論串 (同標題文章)
文章代碼(AID): #1IFhOkv1 (PHP)