PHP magic Method

Source: Internet
Author: User

PHP treats all methods starting with _ (two underscores) in the class as magic methods. It is generally recommended that you do not add _ as the prefix before the custom method.

Magic method:

1.
_ Construct ()

The default constructor of the class. If _ construct () and a method with the same name as the class appear together, _ construct () is called by default instead of the same name method. Generally, the User-Defined constructor uses _ construct ().
2. _ destruct ()
Class destructor, which is executed when all references to the object are deleted or the object is explicitly destroyed.
3. _ Get ($ name)
It can be simply summarized as: If attribute a exists and is of the Public type when the $ object-> A method is used to read the attributes of an object, the value of this attribute is directly returned; if attribute a does not exist or is not directly accessible, the _ Get ($ name) method is called and the return value prevails. You can use this method to restrict external access to internal properties, or perform reflection operations similar to those in Java.
4. _ set ($ name, $ value)
And_ Get ($ name)Similarly, when you assign a value to an attribute in the format of $ object-> A = 17, if attribute a exists and is public, you can directly assign a value to attribute; if attribute a does not exist or is protected/private, the _ set ($ name, $ value) method is called.
5. _ call ($ name, $ arguments)/_ callstatic ($ name, $ arguments)
When a non-existent or inaccessible method is called, The _ call ($ name, $ arguments) method is called. When a non-existing or inaccessible method is called in a static method, the _ callstatic ($ name, $ arguments) method is called.
6. _ tostring ()
It is called directly when an object is printed. Such as Echo $ object;
7. _ clone ()
It is called directly when an object is copied. For example, $ A = new action (); $ A = $ object;
8. _ isset ($ name)/_ unset ($ name)
When isset () or empty () is used for non-existent or inaccessible attributes, __isset () is called. When unset is a non-existent or inaccessible attribute, __unset () will be called. Otherwise, this attribute can be directly unset.
9. _ set_state ()
When var_export () is used to output an object, __set_state () is called. The output content is subject to the return value of this magic method. Note: var_export () and var_dump () are similar, but the content output by var_export () conforms to the PHP syntax. Note:

$ Test = new test ();
$ B = var_export ($ test, true );
Var_dump ($ B );

Class test {

Public $;
Public static function _ set_state ($ array ){
$ AB = new test ();
$ AB-> A = 10;
Return $ AB;
}
}

10. _ autoload ($ name)
When instantiating an object, if the corresponding class does not exist, this method is called. Note: This method is a global function and the parameter is the name of the class.
11. _ sleep ()/_ wakup ()
.

Magic constant:
1. _ line __
Returns the current row number in the file.
2. _ file __
Returns the complete path of the file.
3. _ function __
The name of the function to be returned.
4. _ class __
The name of the class to be returned.
5. _ method __

Returns the name of the class method. Note that _ method _ returns the form of "Class: function", while _ function _ returns the form of "function.


From: http://www.cppblog.com/amazon/archive/2011/10/10/157950.html

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.