Home  >  Article  >  Backend Development  >  php magic constant tutorial

php magic constant tutorial

angryTom
angryTomOriginal
2019-07-16 10:04:022160browse

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.

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 .

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)

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.

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 方法的类的名字。

";     //输出类名
echo  '函数名为:' . __FUNCTION__ ;        //输出方法名
}
}
$t = new test();    //创建对象,调用方法
$t->_print();
?>

运行效果如图所示

php magic constant tutorial

__METHOD__

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

";
echo  '__FUNCTION__:' . __FUNCTION__. "
" ; echo '__METHOD__:' .__METHOD__ ; } } $t = new test(); $t->_print(); ?>

运行效果如图所示

php magic constant tutorial

__NAMESPACE__

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

运行效果如图所示

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!

Statement:
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