Home > Backend Development > PHP Tutorial > php magic constant tutorial

php magic constant tutorial

angryTom
Release: 2023-04-07 06:16:01
Original
2348 people have browsed it

What are magic constants in PHP? Magic constants are predefined constants in PHP that can change depending on where they are used. This article will introduce you to PHP magic constants and introduce the basic usage and functions of magic constants.

PHP provides a large number of predefined constants to any script it runs. However, many constants are defined by different extension libraries and will only appear when these extension libraries are loaded, either dynamically loaded or included at compile time. There are several magic constants whose values ​​change depending on their location in the code.

__LINE__

The current line number in the file.

<?php
header("Content-type:text/html;charset=utf-8");
echo &#39;这是第 " &#39;  . __LINE__ . &#39; " 行&#39;;
?>
Copy after login

The running effect is as shown in the figure

php magic constant tutorial

##__FILE__

The full path and file name of the file. If used within an included file, returns the name of the included file.

Since PHP 4.0.2, __FILE__ always contains an absolute path (or the resolved absolute path if it is a symbolic link), while versions before that sometimes contained a relative path .

<?php
header("Content-type:text/html;charset=utf-8");
echo &#39;该文件位于 " &#39;  . __FILE__ . &#39; " &#39;;
?>
Copy after login

The running effect is shown in the figure


php magic constant tutorial

##__DIR__

The directory where the file is located. If used within an included file, returns the directory where the included file is located. It is equivalent to dirname(__FILE__). Directory names do not include the trailing slash unless they are the root directory. (New in PHP 5.3.0)

<?php
header("Content-type:text/html;charset=utf-8");
echo &#39;该文件位于 " &#39;  . __DIR__ . &#39; " &#39;;
?>
Copy after login

The running effect is as shown in the figure


php magic constant tutorial

##__FUNCTION__

Returns the function name (newly added in PHP 4.3.0).

Since PHP 5, this constant returns the name of the function when it was defined (case-sensitive). In PHP 4 this value is always lowercase.

<?php
header("Content-type:text/html;charset=utf-8");
function test() {
echo  &#39;函数名为:&#39; . __FUNCTION__ ;
}
test();
?>
Copy after login

The operation effect is shown in the figure


__CLASS__

返回类的名称(PHP 4.3.0 新加)。

自 PHP 5 起本常量返回该类被定义时的名字(区分大小写)。在 PHP 4 中该值总是小写字母的。类名包括其被声明的作用区域(例如 Foo\Bar)。注意自 PHP 5.4 起 __CLASS__ 对 trait 也起作用。当用在 trait 方法中时,__CLASS__ 是调用 trait 方法的类的名字。

<?php
header("Content-type:text/html;charset=utf-8");
class test {
function _print() {
echo &#39;类名为:&#39;  . __CLASS__ . "<br>";     //输出类名
echo  &#39;函数名为:&#39; . __FUNCTION__ ;        //输出方法名
}
}
$t = new test();    //创建对象,调用方法
$t->_print();
?>
Copy after login

运行效果如图所示

php magic constant tutorial

__METHOD__

类的方法名(PHP 5.0.0 新加)。返回该方法被定义时的名字(区分大小写)。

<?php
header("Content-type:text/html;charset=utf-8");
class test {
function _print() {
echo &#39;类名为:&#39;  . __CLASS__ . "<br>";
echo  &#39;__FUNCTION__:&#39; . __FUNCTION__. "<br>" ;
echo  &#39;__METHOD__:&#39; .__METHOD__ ;
}
}
$t = new test();
$t->_print();
?>
Copy after login

运行效果如图所示

php magic constant tutorial

__NAMESPACE__

当前命名空间的名称(区分大小写)。此常量是在编译时定义的(PHP 5.3.0 新增)。

<?php
namespace MyProject;
header("Content-type:text/html;charset=utf-8");
echo &#39;命名空间为:"&#39;, __NAMESPACE__, &#39;"&#39;; // 输出 "MyProject"
?>
Copy after login

运行效果如图所示

php magic constant tutorial

如果你想了解更多关于PHP的知识可以到网站的php教程中去学习其他更有趣的知识。

The above is the detailed content of php magic constant tutorial. 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