不能讓我的頭在這個,有沒有什麼辦法來檢查一個方法是否被內部調用?通過這個我的意思是回溯來檢查它是否被$ this調用,而不是指向實例的指針。有點像私人功能的概念,但只有功能是公共的?PHP,區分內部和外部類的方法調用
<?php
class Foo {
public function check() {
/*
if invoked by $this (internally)
return true
else
return false
*/
}
public function callCheck() {
/* returns true because its called by $this */
return $this->check();
}
}
$bar = new Foo;
// this should return false because we are calling it from an instance
$bar->check();
// where as this will return true
$bar->callCheck();
?>
也許這是可以撤消的,但我真的需要它在我的大學項目?任何人都會遇到一個解決方案,或知道如何識別解決方案。
謝謝。
看起來像你應該更換你的接口。函數不應該根據*如何調用而作出不同的反應。如果您正在尋找'public' /'private'行爲,爲什麼不使用它? – deceze 2011-03-25 03:42:42
你爲什麼想知道其中的差異?您似乎可能會嘗試用錯誤的解決方案來解決問題。 – Jonah 2011-03-25 03:43:18
@deceze @Jonah我試圖將對象合併在一起,但保持分開。所以我可以通過編程擴展對象,而不是在界面中定義它。所以如果一個對象有一個受保護的函數,那麼合併它的對象可以調用受保護的函數,但顯然受保護意味着你可以從一個實例調用它。你懂我的意思嗎??? – 2011-03-25 04:10:01