如何在 PHP 中获取当前函数名?
要获取 PHP 中的当前函数名,代码如下 −
示例
<?php class Base { function display() { echo "
Base class function declared final!"; var_dump(__FUNCTION__); } function demo() { echo "
Base class function!"; } } class Derived extends Base { function demo() { echo "
Derived class function!"; } } $ob = new Base; $ob->demo(); $ob->display(); $ob2 = new Derived; $ob2->demo(); $ob2->display(); ?>
输出
这将产生以下输出−
Base class function! Base class function declared final!string(7) "display" Derived class function! Base class function declared final!string(7) "display"
示例
现在,让我们看另一个示例 −
<?php class Base { function display() { echo "
Base class function declared final!"; var_dump(__FUNCTION__); } function demo() { echo "
Base class function!"; var_dump(__METHOD__); } } class Derived extends Base { function demo() { echo "
Derived class function!"; } } $ob = new Base; $ob->demo(); $ob->display(); $ob2 = new Derived; $ob2->demo(); $ob2->display(); ?>
输出
这将产生以下输出−
Base class function!string(10) "Base::demo" Base class function declared final!string(7) "display" Derived class function! Base class function declared final!string(7) "display"
广告