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 '这是第 " ' . __LINE__ . ' " 行'; ?>
The running effect is as shown in the figure
##__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 '该文件位于 " ' . __FILE__ . ' " '; ?>
##__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 '该文件位于 " ' . __DIR__ . ' " '; ?>
The running effect is as shown in the figure
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 '函数名为:' . __FUNCTION__ ; } test(); ?>
__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 '类名为:' . __CLASS__ . "<br>"; //输出类名 echo '函数名为:' . __FUNCTION__ ; //输出方法名 } } $t = new test(); //创建对象,调用方法 $t->_print(); ?>
运行效果如图所示
__METHOD__
类的方法名(PHP 5.0.0 新加)。返回该方法被定义时的名字(区分大小写)。
<?php header("Content-type:text/html;charset=utf-8"); class test { function _print() { echo '类名为:' . __CLASS__ . "<br>"; echo '__FUNCTION__:' . __FUNCTION__. "<br>" ; echo '__METHOD__:' .__METHOD__ ; } } $t = new test(); $t->_print(); ?>
运行效果如图所示
__NAMESPACE__
当前命名空间的名称(区分大小写)。此常量是在编译时定义的(PHP 5.3.0 新增)。
<?php namespace MyProject; header("Content-type:text/html;charset=utf-8"); echo '命名空间为:"', __NAMESPACE__, '"'; // 输出 "MyProject" ?>
运行效果如图所示
如果你想了解更多关于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!