SO問題Weird behaviour with triggering __callStatic() from non-static method非常棒,因爲它解釋了__callStatic沒有從類本身中調用的奇怪行爲(請注意,我在5.3.3中看不到這種行爲,但是在5.3.8和5.3.12)。似乎__callStatic只能從類外部調用。現在這是事實。但是,如果我真的想在我的課堂內調用__callStatic,我該怎麼辦?我應該用什麼語法來解決這個問題?__callStatic方法的神祕行爲
回答
它不必從課外,而不是從對象上下文(即其中$this
是該課程的一個實例)。所以,你可以在一個靜態方法包裝這個調用,例如:
class TestCallStatic
{
public function __call($func, $args)
{
echo "__call($func)";
}
public static function __callStatic($func, $args)
{
echo "__callStatic($func)";
}
public function test()
{
self::_test();
}
protected static function _test()
{
self::i_am_static();
}
}
$test = new TestCallStatic();
$test->test();
輸出:
__callStatic(i_am_static)
這是解決方案。這是要理解你想調用靜態函數的PHP解釋器。通過使用助手靜態函數_test,您可以避免模糊的調用。我的代碼唯一的區別是需要使用$ this,所以:self :: _ test($ this);和靜態函數_test($ myClassInstance) –
我會嘗試將需要'$ this'的部分從不是的部分(僅將「static」部分移動到'_test') - 如果這對您的類是可能的。它使封裝更容易,代碼看起來更乾淨。 –
5.3.3,5.3.8和5.3.12測試成功 –
您可以將功能抽象爲另一種方法,如Class :: magicCall($ method,$ args),並從__callStatic()中調用該方法。這樣,您也可以通過直接調用Class :: magicCall()來訪問該功能。
- 1. 神祕行爲殼
- 2. printf的神祕行爲
- 3. Rails.logger神祕的行爲
- 4. pySerial的神祕行爲
- 5. php的神祕行爲
- 6. static_cast的神祕行爲
- 7. 神祕的Git行爲
- 8. MATLAB神奇的神祕計時行爲
- 9. ruby const_missing方法神祕
- 10. Silverlight回調神祕行爲
- 11. CSS浮動行爲神祕
- 12. 神祕的串行端口行爲
- 13. 與sizeWithFont方法的神祕麻煩
- 14. Python的方法分辨率神祕
- 15. 神祕的JavaScript行爲:不等於
- 16. 顯示神祕行爲的函數
- 17. 向量的shared_ptrs行爲神祕
- 18. 神祕的SQL訪問行爲
- 19. 在try-except-finally中的神祕行爲
- 20. 神祕的C++語法
- 21. 神祕的語法onClick = {:: this.submit}
- 22. 神祕的MFSourceFilter
- 23. 神祕的Windows.UI.Xaml.Markup.XamlParseException
- 24. 神祕的verifier.dll
- 25. 神祕的getClobVal()
- 26. Android SQLite「神祕」查詢行爲
- 27. GridView滾動神祕行爲後notifyDataSetChanged()
- 28. 完全神祕 - 隨機延遲後執行iOS方法
- 29. 神祕間距
- 30. 神祕int值
您可以包括測試情況? – Tchoupi