PHP Magic Function and Magic constants Introduction _php Tutorial

Source: Internet
Author: User

Magic function

    1. __construct ()
    2. Called when an object is instantiated, and when __construct and a function with the name of the class name are present, __construct is called and the other is not called.

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

    5. __call ()
    6. The object calls a method, and if the method exists, it is called directly; if it does not exist, it will call the __call function.

    7. __get ()
    8. When a property of an object is read, the property value is returned directly if the property exists, or the __get function is called if it does not exist.

    9. __set ()
    10. When a property of an object is set, the value is assigned directly if the property exists, or the __set function is called if it does not exist.

    11. __tostring ()
    12. Called when an object is printed. such as Echo $obj; or print $obj;

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

    15. __sleep ()
    16. 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.

    17. __wakeup ()
    18. Unserialize is called to do some initialization of the object.

    19. __isset ()
    20. Called when detecting whether an object's properties exist. such as: Isset ($c->name).

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

    23. __set_state ()
    24. Called when the Var_export is called. Use the return value of __set_state as the return value of Var_export.

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

Magic Constants

    1. __line__
    2. Returns the current line number in the file.

    3. __file__
    4. Returns the full path and file name of the file. If used in the include file, the include filename is returned. Since PHP 4.0.2, __file__ always contains an absolute path, and the previous version sometimes contains a relative path.

    5. __function__
    6. Returns 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.

    7. __class__
    8. Returns 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.

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

The method of first knowledge magic

Php5.0 has provided us with a lot of object-oriented features since its release, especially for our easy-to-use magic methods that allow us to simplify our coding and better design our systems. Today we will come to know the Magic method that php5.0 offers us.

__get () is called when attempting to read a property that does not exist.

If you try to read a property that does not exist for an object, PHP will give you an error message. If you add a __get method to a class, and we can use this function to implement various actions like reflection in Java.

Class Test{public function __get ($key) {  echo $key. "does not exist";}} $t = new Test (); Echo $t->name;

It will output: name does not exist

__set () is called when an attempt is made to write a value to a property that does not exist.

Class Test{public function __set ($key, $value) {  echo ' pair '. $key. "Attached value". $value;}} $t = new Test (), $t->name = "Aninggo";

Will output: The value of the name attached to the Aninggo

__call () Call this method when attempting to invoke a method that does not exist for an object.

Class Test{public function __call ($Key, $Args) {  echo "the {$Key} method you are calling does not exist. The parameter you passed in is: ". Print_r ($Args, True);}} $t = new Test (); $t->getname (Aning,go);

The program will output: The GetName method you want to call does not exist.

The parameters are:

Array ([0] = aning[1] = = Go)

__tostring () is called when an object is printed

This method is similar to the ToString method of Java, when we directly print the object callback with this function

Class Test{public function __tostring () {  return "Print Test";}} $t = new Test (); Echo $t;

When the Echo $t is run, $t->__tostring () is called and output: print Test

__clone () is called when the object is cloned

Class Test{public function __clone () {  echo "I've been copied!" ";}} $t = new Test (), $t 1 = clone $t;

Program output: I've been copied.

http://www.bkjia.com/PHPjc/752480.html www.bkjia.com true http://www.bkjia.com/PHPjc/752480.html techarticle 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. __destr ...

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