問題描述

我不知道我是否正確地做到這一點。

據我瞭解:

如果我有一個類 foo 和一個靜態方法欄,我可以通過傳遞數組 array("foo","bar")作為函數名稱將其註冊為回調。

如果我在 $ foo 中有一個類的實例,並且想要調用方法欄,我傳遞數組 array($foo,'bar')

如果我需要在課堂內註冊一個動作,它會與 array($this,'bar')一起使用嗎?

最佳解決方案

If I need to register an action inside the class itself would it work with array($this, 'bar')?

是的,它有效。 $this 文檔是指 callback 所需的具體實例。這就像你給的 $foo 例子。只是 $this 更特別,但它代表基本相同,它在 PHP 中的回調非常有效。

額外:

if I have a class foo and a static method bar I can register that as the callback by passing the array array("foo","bar") as the function name.

是的,您可以這樣做,對於靜態功能,您可以將其寫為字符串而不是數組:foo::bar,請參閲回調文檔。可能很方便

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。