What does PHP's magic variable mean?

Source: Internet
Author: User

PhpMagic Variable

PHP provides a large number of predefined constants to any script it runs.

However, many constants are defined by different extensions, and are only present when the extensions are loaded, either dynamically after loading, or they are included at compile time.

There are eight magic constants whose values change as they change position in the code.

For example, the value of __line__ depends on the row it is in the script to determine. These special constants are case-insensitive, as follows:

__line__

The current line number in the file.

Instance<?php echo ' This is the first ' . __line__ . ' the line '; ?>

The result of the above example output is:

This is the  "2" line.   
__file__

The full path and file name of the file. If used in the included file, returns the file name that is included.

From PHP 4.0.2, __file__ always contains an absolute path (if it is a symbolic connection, the resolved absolute path), and the previous version sometimes contains a relative path.

Instance:

Instance<?php echo ' This file is located in ' . __file__ . " '; ?>

The result of the above example output is:

The file is located in  " E:\wamp\www\test\index."     
__dir__

The directory where the file resides. If used in the included file, returns the directory where the included files are located.

It is equivalent to DirName (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0)

Instance<?php echo ' This file is located in ' . __dir__ . " '; ?>

The result of the above example output is:

The file is located in the  " E:"   
__function__

The name of the function (PHP 4.3.0 new addition). From PHP 5 This constant returns the name (case-sensitive) when the function is defined. In PHP 4, this value is always in lowercase letters.

Instance The <?php function Test () { echo ' functions is named:' . __function__ ; } Test(); ?>

The result of the above example output is:

The function name is:test
__class__

The name of the class (PHP 4.3.0 new addition). From PHP 5 This constant returns the name of the class when it is defined (case-sensitive).

In PHP 4, this value is always in lowercase letters. The class name includes its declared action area (for example, Foo\bar). Note since PHP 5.4, __CLASS__ has also worked for trait. When used in the trait method, __class__ is the name of the class that invokes the trait method.

Instance<?php Class Test { function _print() { Echo ‘The class name is:‘.__class__."<br> "; echo " ' __function__; } } $ T = new test< Span class= "Hl-brackets" > ( $t ->_print (?>

The result of the above example output is:

The class name is: Thetest function is named:_print  
__trait__

Trait's name (PHP 5.4.0 new Plus). Since PHP 5.4.0, PHP has implemented a method of code reuse, called traits.

The Trait name includes its declared function area (for example, Foo\bar).

Members inherited from the base class are overwritten by the Myhelloworld method in the inserted Sayworld Trait. Its behavior is consistent with the methods defined in the Myhelloworld class. The precedence is that methods in the current class override the Trait method, and the trait method overrides the method in the base class.

Instance<?php Class Base { Public function SayHello() { Echo ‘Hello‘;} } Trait Sayworld { Public function SayHello() { Parent::SayHello();Echo ‘world!‘;} } Class Myhelloworld extends Base {use sayworld; } $o = new myhelloworld(); $o-SayHello(); ?>

The above routines will output:

Hello World! 
__method__

The method name of the class (PHP 5.0.0 new addition). Returns the name of the method when it is defined (case-sensitive).

Instance:

Instance The <?php function Test () { echo ' functions is named:' . __method__ ; } Test(); ?>

The result of the above example output is:

The function name is:test
__namespace__

The name of the current namespace (case-sensitive). This constant is defined at compile time (PHP 5.3.0 is new).

Instance:

Instance<?php namespace MyProject; echo ' namespaces are: '', __namespace__, ' ' ; // output "MyProject" ?>

The result of the above example output is:

The namespace is:"MyProject"

What does a PHP magic variable mean

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.