Home  >  Article  >  Backend Development  >  What are magic constants in PHP

What are magic constants in PHP

青灯夜游
青灯夜游Original
2019-01-25 17:38:353912browse

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 some commonly used PHP magic constants. I hope it will be helpful to you.

What are magic constants in PHP

Magic constants start with two underscores (__) and end with two underscores (__). Below we will introduce some commonly used PHP magic constants.

__LINE__

__LINE__ constant returns the current line number of the file. Example:

<?php
header("content-type:text/html;charset=utf-8");
echo "当前行数:" . __LINE__ . "<br>"; // 第3行
echo "当前行数:" . __LINE__ . "<br>"; // 第4行
echo "当前行数:" . __LINE__ . "<br>"; // 第5行
?>

Output:

What are magic constants in PHP

__FILE__

__FILE__ constant return is The full path and name of the PHP file being executed; if used within an include, returns the name of the included file.

<?php
header("content-type:text/html;charset=utf-8");
// 显示此文件的绝对路径
echo "此文件的完整路径是: " . __FILE__;
?>

Output:

What are magic constants in PHP

__DIR__

__DIR__ constant returns the directory of the file . If used within an include, returns the directory containing the file. Example:

<?php
header("content-type:text/html;charset=utf-8");
// 显示此文件的目录
echo "此文件的目录是: " . __DIR__;
?>

Output:

What are magic constants in PHP

##__FUNCTION__
## The #__FUNCTION__ constant returns the name of the current function. Example:

<?php
header("content-type:text/html;charset=utf-8");
function myFunction(){
    echo  "函数名是:" . __FUNCTION__;
}
myFunction(); 
?>

Output:


What are magic constants in PHP

##__CLASS__

__CLASS__ Constant returns the name of the current class.

<?php
header("content-type:text/html;charset=utf-8");
class MyClass
{
    public function getClassName(){
        return __CLASS__;
    }
}
$obj = new MyClass();
echo $obj->getClassName(); 
?>

Output:


What are magic constants in PHP

__METHOD__

__METHOD__ constant return The name of the current class method.

<?php
header("content-type:text/html;charset=utf-8");
class Sample
{
    public function myMethod(){
        echo __METHOD__;
    }
}
$obj = new Sample();
$obj->myMethod(); 
?>

Output:


What are magic constants in PHP

__NAMESPACE__

__NAMESPACE__constant return The name of the current namespace.

<?php
// 定义代码在 &#39;MyNamespace&#39; 命名空间中
namespace MyNamespace;
class MyClass
{
    public function getNamespace(){
        return __NAMESPACE__;
    }
}
$obj = new MyClass();
echo $obj->getNamespace(); // Displays: MyNamespace
?>

Output:


The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! ! What are magic constants in PHP

The above is the detailed content of What are magic constants in PHP. 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