This article mainly introduces the usage of php magic variables, analyzes in detail the usage of various common magic variables in php in the form of examples, and introduces the magic methods in php, has good reference value and needs of friends
This article mainly introduces the usage of php magic variables, analyzes in detail the usage of various common magic variables in php in the form of examples, and introduces the magic methods in php, has good reference value and needs of friends
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