Php 简明教程

PHP - Magic Constants

PHP 中的魔术常量是预定义常量。它们可以供在上面运行的任何脚本使用,并且它们会根据使用位置而改变。与在运行时解决的常规常量不同,所有这些“魔术”常量均在编译时解决。

The magical constants in PHP are predefined constants. They are available to any script on which they run, and they change depending on where they are used. All these "magical" constants are resolved at compile time, unlike regular constants, which are resolved at runtime.

PHP 中有九个魔术常量。这些特殊常量不区分大小写。

There are nine magical constants in PHP. These special constants are case insensitive.

LINE

它返回文件中的当前行号。以下 example 展示了如何使用此魔术常量。

It returns the current line number of the file. The following example shows how you can use this magic constant.

<?php
   $x="Hello World";
   echo "$x. The current Line number is " . __LINE__ . ".";
?>

它将生成以下 output

It will produce the following output

Hello World. The current Line number is 5.

FILE

此魔术常量返回文件的完整路径和文件名。如果在 include 内使用,则返回已包括文件的名字。请看以下 example

This magic constant returns the full path and filename of the file. If used inside an include, the name of the included file is returned. Take a look at the following example

<?php
   $x="Hello World";
   echo "$x. Current PHP script name is " . __FILE__ . ".";
?>

它将生成以下 output

It will produce the following output

Hello World. Current PHP script name is C:\xampp\htdocs\hello.php.

DIR

此魔术常量返回文件的目录。如果在 include 内使用,则返回已包括文件的目录。这相当于“dirname( FILE )”。此目录名称没有尾随斜杠,除非它是根目录。见以下 example

This magical constant returns the directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to "dirname(FILE)". This directory name does not have a trailing slash unless it is the root directory. See the following example

<?php
   $x="Hello World";
   echo "$x. Directory of the Current PHP script name is " . __DIR__ . ".";
?>

它将在浏览器上显示以下 output

It will show the following output on the browser −

Hello World. Directory of the Current PHP script name is C:\xampp\htdocs.

FUNCTION

此魔术常量返回使用常量的功能名称,或匿名功能的 {closure}。以下 example 展示了它是如何工作的 −

This magical constant returns the function name in which the constant is used, or {closure} for anonymous functions. The following example shows how it works −

<?php
   function hello(){
      $x="Hello World";
      echo "$x. The function name is ". __FUNCTION__ . "";
   }
   hello();
?>

它将生成以下 output

It will produce the following output

Hello World. The function name is hello

如果此魔术常量在功能之外使用,那么它将给空白输出。

If this magic constant is used outside the function, then it will give a blank output.

CLASS

此常量返回类名称。类名称包含它声明的命名空间。见以下 example

This constant returns the name of a class. The class name includes the namespace it was declared in. See the following example

<?php
   class myclass {
      public function __construct() {
         echo "Inside the constructor of ". __CLASS__ . PHP_EOL;
      }
      function getClassName(){
         echo "from an instance method of " . __CLASS__ . "";
      }
   }
   $obj = new myclass;
   $obj->getClassName();
?>

它将生成以下 output

It will produce the following output

Inside the constructor of myclass
from an instance method of myclass

METHOD

METHOD 常量返回类方法名称。以下 example 展示了它是如何工作的 −

The METHOD constant returns the class method name. The following example shows how it works −

<?php
   class myclass {
      public function __construct() {
         echo "Calling " . __METHOD__ . " of " . __CLASS__ ."<br>";
      }
      function mymethod(){
         echo "Calling " . __METHOD__ . " of " . __CLASS__ ."";
      }
   }
   $obj = new myclass;
   $obj->mymethod();
?>

它将生成以下 output

It will produce the following output

Calling myclass::__construct of myclass
Calling myclass::mymethod of myclass

TRAIT

返回性状名称。性状名称包含它声明的命名空间。在 PHP 中,性状是重新使用代码的机制。性状类似于类,但只准备以精细且一致的方式对功能分组。无法在它自己上实例化这种性状。

It returns the trait name. The trait name includes the namespace it was declared in. In PHP, traits are a mechanism for code reuse. A trait is similar to a class, but only intended to group functionality in a fine-grained and consistent way. It is not possible to instantiate a trait on its own.

看看以下 example

Take a look at the following example

<?php
   trait mytrait {
      public function hello() {
         echo "Hello World from " . __TRAIT__ ."";
      }
   }
   class myclass {
      use mytrait;
   }
   $obj = new myclass();
   $obj->hello();
?>

它将生成以下 output

It will produce the following output

Hello World from mytrait

NAMESPACE

此常量返回当前命名空间的名称。在 PHP 中,通过命名空间,我们能够使用名称相同的类 / 功能 / 常量在不同的环境中,无需任何冲突,从而封装这些项目。命名空间是类 / 功能的逻辑组合,根据它们的相关性。

This constant returns the name of the current namespace. In PHP, namespaces allow us to use classes / functions / constants of same name in different contexts without any conflict, thereby encapsulating these items. A namespace is a logical grouping of classes/functions depending on their relevance.

以下 example 展示了如何使用此魔术常量 −

The following example shows how you can use this magic constant −

<?php
   namespace myspace;
   class myclass {
      public function __construct() {
         echo "Name of the class: " . __CLASS__ . " in " . __NAMESPACE__ . "";
      }
   }
   $class_name = __NAMESPACE__ . '\myclass';
   $a = new $class_name;
?>

它将生成以下 output

It will produce the following output

Name of the class: myspace\myclass in myspace

ClassName::class

与其他魔术常量不同,这个魔术常量不以双下划线 (__ ) 开头或结尾。它返回完全限定的类名。

Unlike the other magic constants, this magic constant does not start and end with the double underscore (__). It returns the fully qualified class name.

以下 example 展示了如何使用此魔术常量 −

The following example shows how you can use this magic constant −

<?php
   namespace myspace;
   class myclass {
      public function __construct() {
         echo "Name of the class: " . myclass::class ;
      }
   }
   use myspace;
   $a = new myclass;
?>

它将生成以下 output

It will produce the following output

Name of the class: myspace\myclass