获取方法:1、使用“__function__”,可获取当前方法的名称;2、使用“__method__”,可获取当前的方法名(包括类名);3、使用get_class_methods()函数,可获取指定类的所有的方法名。
本教程操作环境:windows7系统、php7.1版、dell g3电脑
php获取方法名称
1、使用魔术常量__function__
__function__:当前函数(或方法)的名称;
<?phpheader("content-type:text/html;charset=utf-8");class website { public function demo() { echo '成员方法名'.__function__; }}$student = new website();$student -> demo();?>
2、使用魔术常量__method__
__method__:当前的方法名(包括类名);返回该方法被定义时的名字(区分大小写)。
<?phpheader("content-type:text/html;charset=utf-8");class website { public function demo() { echo '类名+方法名'.__method__; }}$student = new website();$student -> demo();?>
3、get_class_methods()函数
get_class_methods — 获取类的所有的方法名,并且组成一个数组
get_class_methods(mixed $class_name): array
返回由 class_name 指定的类中定义的方法名所组成的数组。如果出错,则返回 null。
示例:
<?phpclass myclass { // constructor function myclass() { return(true); } // method 1 function myfunc1() { return(true); } // method 2 function myfunc2() { return(true); }}$class_methods = get_class_methods('myclass');// or$class_methods = get_class_methods(new myclass());foreach ($class_methods as $method_name) { echo "$method_name <br>";}?>
推荐学习:《php视频教程》
以上就是php怎么获取方法名称的详细内容。