[請益] callback與匿名函式
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是否完全貼切?)的限制。
最後一句「To reflect...anonymous function」,我推測應該是因為前面提到函式有
「localized and temporary」的特性,才提到匿名函式可以因應callback的特性。
反覆思考後,還是想不太出來「transient nature」是什麼意思,transient在字典裡是
「短暫的; 一時的; 瞬間的」等意思,感覺都搭不太起來。希望有板友能解惑
關於個人解讀的部分,如有錯誤,也請板友多多指教。謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.248.90.167
※ 編輯: edash 來自: 111.248.90.167 (09/22 15:15)
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 2 篇):
PHP 近期熱門文章
PTT數位生活區 即時熱門文章