Home >Backend Development >PHP Problem >How to get the current class name and method name in php
Getting method: 1. Use the magic constant "__CLASS__" to get the current class name (including the scope or namespace of the class); 2. Use "__FUNCTION__" to get the name of the current method; 3. Use "__METHOD__" to get the current method name (including class name).
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you can use magic constants to obtain Class name and method name.
Magic constants are special predefined constants that can change depending on where they are used. Magic constants usually start with two underscores __ and end with two underscores __.
The magic constants that can get the class name and method name are: "__CLASS__", "__FUNCTION__" and "__METHOD__"
__CLASS__
: The current class name (including the scope or namespace of the class);
Since PHP 5, this constant returns the name when the class was defined (case-sensitive). In PHP 4 this value is always lowercase.
__FUNCTION__
: The name of the current function (or method);
##__METHOD__: Current method name (including class name);
<?php header("Content-type:text/html;charset=utf-8"); class Website { public function demo() { echo '类名'.__CLASS__."<br>"; echo '成员方法名'.__FUNCTION__."<br>"; echo '类名+方法名'.__METHOD__; } } $student = new Website(); $student -> demo(); ?>Output:
类名Website 成员方法名demo 类名+方法名Website::demoRecommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to get the current class name and method name in php. For more information, please follow other related articles on the PHP Chinese website!