Home  >  Article  >  Backend Development  >  How to get the current class name and method name in php

How to get the current class name and method name in php

青灯夜游
青灯夜游Original
2021-09-23 13:58:595057browse

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).

How to get the current class name and method name in php

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);

    Returns the name when the method was defined (case-sensitive).


Example:

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

Output:


类名Website
成员方法名demo
类名+方法名Website::demo

Recommended 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn