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

青灯夜游
Release: 2023-03-12 21:00:02
Original
4978 people have browsed it

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();
?>
Copy after login

Output:


类名Website
成员方法名demo
类名+方法名Website::demo
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!