PHP Magic Variable and Magic Function Example Tutorial summary

Source: Internet
Author: User
    1. Echo ' This is the first '. __line__. ' The line ';
    2. ?>
Copy Code

The output is: This is the "2" line

The full path and file name of the __file__ 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.

Example:

    1. Echo ' The file is located '. __file__. ' ” ';
    2. ?>
Copy Code

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

The directory where the __dir__ 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)

Example:

    1. Echo ' The file is located '. __dir__. ' ” ';
    2. ?>
Copy Code

The output is: The file is in "E:\wamp\www\test"

__function__ function name (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.

Example:

    1. function Test () {
    2. Echo ' function name is: '. __function__;
    3. }
    4. Test ();
    5. ?>
Copy Code

The output is: The function is named: Test

The name of the __class__ 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.

Example:

    1. Class Test {
    2. function _print () {
    3. Echo ' class name is: '. __class__. "
      ";
    4. Echo ' function name is: '. __function__;
    5. }
    6. }
    7. $t = new test ();
    8. $t->_print ();
    9. ?>
Copy Code

The output is: The class name is: The test 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.

Example:

    1. Class Base {
    2. Public Function SayHello () {
    3. Echo ' Hello ';
    4. }
    5. }
    6. {
    7. Public Function SayHello () {
    8. Parent::sayhello ();
    9. Echo ' world! ';
    10. }
    11. }
    12. Class Myhelloworld extends Base {
    13. }
    14. $o = new Myhelloworld ();
    15. $o->sayhello ();
    16. ?>
Copy Code

Output: Hello world!

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

Example:

    1. function Test () {
    2. Echo ' function name is: '. __method__;
    3. }
    4. Test ();
    5. ?>
Copy Code

The output is: The function is named: Test

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

Example:

    1. namespace MyProject;
    2. Echo ' namespaces are: ' ', __namespace__, ' '; Output "MyProject"
    3. ?>
Copy Code

The output is: The namespace is: "MyProject"

The Magic function __construct () is called when an object is instantiated, and when __construct and a function with a class name are present, __construct is called and the other is not called. __destruct () is called when an object is deleted or when an object operation terminates. The __call () object invokes a method that is called directly if the method exists, or calls the __call function if it does not exist. __get () When the property of an object is read, the property value is returned directly if it exists, and the __get function is called if it does not exist. __set () Sets the property of an object, assigns the value directly if the attribute exists, or calls the __set function if it does not exist. __tostring () is called when an object is printed. such as Echo $obj, or print $obj; __clone () is called when the object is cloned. such as: $t =new Test (), $t 1=clone $t, __sleep () serialize before being called. If the object is relatively large, want to cut a bit of the east and then serialize, you can consider this function. __wakeup () Unserialize is called to do some initialization of the object. __isset () is called when detecting whether an object's properties exist. such as: Isset ($c->name). __unset () is called when a property of an object is unset. such as: unset ($c->name). Called when the __set_state () call is Var_export. Use the return value of __set_state as the return value of Var_export. __autoload () When an object is instantiated, the method is called if the corresponding class does not exist.

  • 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.