Use magic method __CLASS__ in PHP to get related operations of class name

jacklove
Release: 2023-03-30 18:38:02
Original
3248 people have browsed it

The related operations of magic method __CLASS__ to obtain the class name are often encountered in PHP. This article will explain its related operations.

Tutorial on using the magic method __CLASS__ to obtain the class name in PHP

The official document is as follows

__CLASS__The name of the class (newly added in PHP 4.3.0). Since PHP 5 this constant returns the name of the class when it was defined (case sensitive). In PHP 4 this value is always lowercase. The class name includes the scope in which it is declared (e.g. Foo\Bar). Note that since PHP 5.4 CLASS also works for trait

. When used within a trait method, CLASS is the name of the class that calls the trait method.

If you want to get the name of the scope that does not include the class or the scope of the class, you need the following two functions

string basename ( string $path [, string $suffix ] )
Copy after login

//Give a file containing the full path to a file String, this function returns the directory name after removing the file name.

string dirname ( string $path )
Copy after login

//Given a string containing the full path to a file, this function returns the basic file name.

The name of the class contains the scope

echo __CLASS__;
Copy after login

The name of the class scope

echo dirname(__CLASS__);
Copy after login

The name of the class does not contain the scope

echo basename (__CLASS__);
Copy after login

This article explains PHP Use the magic method __CLASS__ to obtain the related operations of the class name. For more related content, please pay attention to the php Chinese website.

Related recommendations:

php code implementation 12306 remaining votes query , Price query function

How to obtain the openid and basic information of WeChat users through PHP

Introduction to PHP to quickly export Table Data related tutorial

The above is the detailed content of Use magic method __CLASS__ in PHP to get related operations of class name. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!