Description of the PHP magic function and the Magic constants

Source: Internet
Author: User

Magic function

1. __construct ()

Called when an object is instantiated,
When __construct and a function with a class name are present, __construct is called and the other is not called.

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

3. __call ()
Object calls a method,
If the method exists, it is called directly;
If it does not exist, it will call the __call function.

4. __get ()
When reading the properties of an object,
If the attribute exists, the property value is returned directly;
If it does not exist, the __get function is called.

5. __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.

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

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

8. __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.

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

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

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

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

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

Magic Constants

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

2. __file__
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.

3. __function__
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.

4. __class__
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.

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


(1) First knowledge of magic method
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.

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.