Php magic variable usage example explanation, php magic example explanation _ PHP Tutorial

Source: Internet
Author: User
Examples of php magic variable usage are described in detail, and php magic examples are described in detail. Examples of php magic variable usage are described in detail. Examples of php magic variables are described in this article. _ DIR _ is newly added in php5.3 and will be shared with you for your reference. Php magic variable usage examples and php magic examples

The example in this article describes the usage of php magic variables. _ DIR _ is newly added in php5.3 and will be shared with you for your reference. The usage analysis is as follows:

System constant

_ FILE _ current FILE name
_ LINE _ current number of rows
_ FUNCTION _ current FUNCTION name
_ CLASS _ current CLASS name
_ METHOD name of the current object

Detailed analysis

1. _ FILE __

The complete file path and file name. If it is used in a file to be included, the file name to be included is returned. Starting from PHP 4.0.2, __file _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution ), earlier versions sometimes contain a relative path.
PHP constant dirname (_ file __)
_ FILE _: a PHP magic constant. it returns the complete path and FILE name of the currently executed PHP script, including an absolute path.

1) the dirname (_ FILE _) function returns the path of the script. Update network
For example, file B. php contains the following content:

The code is as follows:

<? Php
$ Basedir = dirname (_ FILE __);
Echo $ basedir
// An absolute path of the file will be printed on the page!
?>



The result of my test is: E: websiteothertestcms.
This is equivalent to the usage of server. mappth in asp.
If B. php is referenced by the. php file require or include in another directory. The content of the variable $ basedir is still the path of the folder where B. php is located. Instead of the directory where the. php file is located.

2) dirname (_ FILE _) usually returns a directory structure from the current directory of the FILE to the root directory of the system.
The current file name is not returned. Dirname (_ FILE _) may also return a. (current directory) [This is because the B. php FILE is in the default WEB directory where http. conf or PHP is used to configure the development environment.

The code is as follows:

<? Php
/**
Set your root directory in your public configuration file, so you don't have to worry about moving frequently.
*/
Define ('root _ path', dirname (_ FILE _). DIRECTORY_SEPARATOR );
Echo ROOT_PATH;
Echo"
";
Echo _ FILE __;
Echo"
";
Echo dirname (_ FILE __);
Echo"
";
Echo dirname (_ FILE __));
?>


2. _ LINE __

The current row number in the file. This variable is useful when debugging errors occur. it is a personal opinion.

The code is as follows:

<? Php
Echo _ LINE __; // Display, __line _ row number
?>


3. _ CLASS __
Class name. The results returned by PHP5 are case sensitive.

The code is as follows:

<? Php
Class base_class
{
Function say_a ()
{
Echo "'A'-said the". _ CLASS __."
";
}
Function say_ B ()
{
Echo "'B'-said the". get_class ($ this )."
";
}
}
Class derived_class extends base_class
{
Function say_a ()
{
Parent: say_a ();
Echo "'A'-said the". _ CLASS __."
";
}
Function say_ B ()
{
Parent: say_ B ();
Echo "'B'-said the". get_class ($ this )."
";
}
}
$ Obj_ B = new derived_class ();
$ Obj_ B-> say_a ();
Echo"
";
$ Obj_ B-> say_ B ();
?>


Result:

The code is as follows:

'A'-said the base_class
'A'-said the derived_class
'B'-said the derived_class
'B'-said the derived_class


Sometimes, we can use get_class instead of _ CLASS __

4. _ FUNCTION _ and _ METHOD __

_ FUNCTION __: FUNCTION name. The result returned in php5 is case sensitive.
_ METHOD __: name of the function in the METHOD. the result returned in php5 is case sensitive.
What are the differences between the two methods?

The code is as follows:

<? Php
Class test
{
Function ()
{
Echo _ FUNCTION __;
Echo"
";
Echo _ METHOD __;
}
}
Function good (){
Echo _ FUNCTION __;
Echo"
";
Echo _ METHOD __;
}
$ Test = new test ();
$ Test-> ();
Echo"
";
Good ();
?>


Returned results:
A
Test:
Good
Good
Compared with an isolated FUNCTION, the two functions can retrieve the FUNCTION name. if it is a method in the class, __function _ can only retrieve the method name of the class, while _ METHOD _ does not only retrieve the METHOD name, but also the class name

5. _ DIR __

The directory where the file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0)
If you want to use _ DIR _ in versions earlier than 5.3, you can do this.

The code is as follows:

<? Php
If (! Defined ('_ DIR __')){
$ IPos = strrpos (_ FILE __,"/");
Define ("_ DIR _", substr (_ FILE __, 0, $ iPos )."/");
}
?>


6. _ NAMESPACE __

Name of the current namespace (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0)

7. _ STATIC __

When you call the static method of class, the class name is returned, which is case sensitive. If it is called in inheritance, the inherited class name can be returned no matter whether it is defined in inheritance.

The code is as follows:

<? Php
// Php5.3
Class Model
{
Public static function find ()
{
Echo _ STATIC __;
}
}
Class Product extends Model {}
Class User extends Model {}
Product: find (); // "Product"
User: find (); // "User"
?>


Supplement: Magic methods in php

_ Construct () when instantiating an object, this method of this object is called first.
_ Destruct () this method is called when an object or object operation is terminated.
_ Get () is called when an attempt is made to read an attribute that does not exist.
_ Set () is called when you try to write a value to an attribute that does not exist.
_ Call () this method is called when you try to call a method that does not exist in an object.
_ ToString () is called when an object is printed
_ Clone () is called when an object is cloned.
_ Isset ()
_ Unset ()
_ Autoload ($ classname)
_ Sleep ()
_ Wakeup ()

I hope this article will help you with php programming.

Examples in this article describe the usage of php magic variables. _ DIR _ is newly added in php5.3 and will be shared with you for your reference. Usage...

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.