PHP Magic Variables and Magic function detailed _php tips

Source: Internet
Author: User
Tags lowercase

Magic 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 available when the extensions are loaded, either dynamically or at compile time.

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

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

__line__

The current line number in the file.

Instance:

Copy Code code as follows:

<?php
Echo ' This is the first '. __line__. ' The line ';
?>

The result of the above example output is:

Copy Code code as follows:

This is the "2" line.

__file__

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

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

Instance:

Copy Code code as follows:

<?php
Echo ' This file is located '. __file__. ' ” ';
?>

The result of the above example output is:

Copy Code code as follows:

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

__dir__

The directory in which 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:

Copy Code code as follows:

<?php
Echo ' This file is located '. __dir__. ' ” ';
?>

The result of the above example output is:

Copy Code code as follows:

The file is located in the "E:\wamp\www\test"

__function__

Function name (PHP 4.3.0 new). From PHP 5 This constant returns the name (case-sensitive) of the function when it is defined. This value is always lowercase in PHP 4.

Instance:

Copy Code code as follows:

<?php
function Test () {
Echo ' function is named: '. __function__;
}
Test ();
?>

The result of the above example output is:

Copy Code code as follows:

Function Name: Test

__class__

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

This value is always lowercase in PHP 4. The class name includes its declared range of actions (for example, Foo\bar). Note that since PHP 5.4 __class__ also works with trait. When used in the trait method, __class__ is the name of the class that invokes the trait method.

Instance:

Copy Code code as follows:

<?php
Class Test {
function _print () {
Echo ' class name is: '. __class__. "<br>";
Echo ' function is named: '. __function__;
}
}
$t = new test ();
$t->_print ();
?>

The result of the above example output is:

Class Name: Test
Function name is: _print

__trait__

Trait's name (PHP 5.4.0 new). Since PHP 5.4.0, PHP implements a code reuse method, called traits.

The Trait name includes the area of action that it declares (for example, Foo\bar).

The member inherited from the base class is 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 the method in the current class overrides the trait method, while the trait method overrides the method in the base class.

Copy Code code as follows:

<?php
Class Base {
Public Function SayHello () {
Echo ' Hello ';
}
}
{
Public Function SayHello () {
Parent::sayhello ();
Echo ' world! ';
}
}
Class Myhelloworld extends Base {

}
$o = new Myhelloworld ();
$o->sayhello ();
?>

The above routines will output:

Copy Code code as follows:

Hello world!

__method__

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

Instance:

Copy Code code as follows:

<?php
function Test () {
Echo ' function is named: '. __method__;
}
Test ();
?>

The result of the above example output is:

Copy Code code as follows:

Function Name: Test

__namespace__

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

Instance:

Copy Code code as follows:

<?php
namespace MyProject;
The echo ' namespace is: ', __namespace__, ' '; Output "MyProject"
?>

The result of the above example output is:

Copy Code code as follows:

The namespace is: "MyProject"

Magic function

__construct ()
is invoked when the object is instantiated,
When __construct and a function with the class name as the function name exist at the same time, the __construct is invoked and the other is not invoked.

__destruct ()
Called when an object is deleted or an object operation terminates.

__call ()
object to invoke a method,
If the method exists, it is called directly;
If it does not exist, it will call the __call function.

__get ()
When you read an object's properties,
If the attribute exists, the property value is returned directly;
If it does not exist, the __get function is called.

__set ()
When you set the properties of an object,
If the attribute exists, the value is directly assigned;
If it does not exist, the __set function is called.

__tostring ()
When an object is printed, it is invoked. such as Echo $obj, or print $obj;

__clone ()
Called when the object is cloned. such as: $t =new Test (); $t 1=clone $t;

__sleep ()
Serialize before being invoked. If the object is relatively large, want to cut a little bit of the serialization, you can consider this function.

__wakeup ()
Unserialize is invoked to do initialization work on some objects.

__isset ()
Called to detect whether an object's properties exist. such as: Isset ($c->name).

__unset ()
Called when a property of an object is unset. such as: unset ($c->name).

__set_state ()
Called when the Var_export is invoked. Use the return value of the __set_state as the return value of the Var_export.

__autoload ()
When an object is instantiated, the method is invoked if the corresponding class does not exist.

The above is the entire content of this article, the small partners whether the magic variable and magic function has a new understanding of it, I hope you like the content of this article.

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.