Home >Backend Development >PHP Tutorial >Use magic method __CLASS__ in PHP to get related operations of class name
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 ] )
//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 )
//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__;
The name of the class scope
echo dirname(__CLASS__);
The name of the class does not contain the scope
echo basename (__CLASS__);
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!