我對OOP相當陌生,我試圖獲取當前正在執行的類和方法的名稱。例如:獲取當前正在執行的方法
<?php
class ParentExample
{
function __construct()
{
echo get_class($this) . '<br />';
echo __METHOD__;
exit;
}
}
class ChildExample extends ParentExample
{
public static function test()
{
echo 'hello';
}
}
call_user_func_array([new ChildExample, test]);
這是我想要的結果:
ChildExample
ChildExample::test
這是結果我得到:
ChildExample
ParentExample::__construct
我看了debug_backtrace(),但我不明白如何解讀結果或者甚至是我所需要的。有沒有更簡單的方法來實現我所追求的目標?
編輯:基於答案,我認爲我的問題不清楚。我想讓ParentExample構造函數告訴我被調用方法的名字。這可能嗎?
我已更新我的問題。是否有可能從父構造函數中獲取被調用的方法? – 2013-05-06 21:48:55
@mistermartin我已經更新了答案。不,它不起作用。 – phant0m 2013-05-06 21:54:25