php如何获取方法名称

发布时间:2022-02-10 13:32:03 作者:iii
来源:亿速云 阅读:245

这篇文章主要介绍了php如何获取方法名称的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇php如何获取方法名称文章都会有所收获,下面我们一起来看看吧。

获取方法:1、使用“__FUNCTION__”,可获取当前方法的名称;2、使用“__METHOD__”,可获取当前的方法名(包括类名);3、使用get_class_methods()函数,可获取指定类的所有的方法名。

php如何获取方法名称

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

php获取方法名称

1、使用魔术常量__FUNCTION__

__FUNCTION__:当前函数(或方法)的名称;

<?php
header("Content-type:text/html;charset=utf-8");
class Website {
	public function demo() {
		echo '成员方法名'.__FUNCTION__;
	}
}
$student = new Website();
$student -> demo();
?>

php如何获取方法名称

2、使用魔术常量__METHOD__

__METHOD__:当前的方法名(包括类名);返回该方法被定义时的名字(区分大小写)。

<?php
header("Content-type:text/html;charset=utf-8");
class Website {
	public function demo() {
		echo '类名+方法名'.__METHOD__;
	}
}
$student = new Website();
$student -> demo();
?>

php如何获取方法名称

3、get_class_methods()函数

get_class_methods — 获取类的所有的方法名,并且组成一个数组

get_class_methods(mixed $class_name): array

返回由 class_name 指定的类中定义的方法名所组成的数组。如果出错,则返回 null。

示例:

<?php

class 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如何获取方法名称”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“php如何获取方法名称”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. php获取访客所在城市名称代码
  2. Powershell 如何获取异常的名称

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php

上一篇:python中怎么对正则表达式re包进行引用

下一篇:swoole进程与线程有哪些区别

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》