PHP Learning Note--10. Magic method

Source: Internet
Author: User

1. Definition

Refers to methods that are automatically called in certain situations

PHP provides a number of magic methods, all starting with __

2. Construction method __construct

This method is called automatically when an object is instantiated
Can be used to instantiate an object Shishun to control the properties of the class

Class Name {
private property name = NULL;
Public function __construct ($ formal parameter) {
$this-Property name = $ formal parameter;
}
}
$ object = new class name (' argument ');
Echo $ object, property name;

3. Destructor Method __destruct

This method is called when the object is destroyed with unset (), another value is assigned to the object, or when the page finishes executing

4. Auto Load __autoload

This method is called automatically when an instantiated class is not loaded into the current file
And the class name is passed in as a parameter

function __autoload ($class) {
Require ('./'. $class. php ');
}
$ object = new class name ();

Attention:
The declaration of a class is globally valid (even inside the function)
You can use Spl_autoload_regsite (' method name ') to customize the method
Register as a method with the "Auto-load" feature

4. Other
Method name Invocation Criteria
__clone () When cloning an object using Clone ()
__get ($ formal parameter) when accessing non-public/undefined properties
__set ($ parameter 1,$ parameter 2) when modifying non-public/Add undefined properties
__call ($ parameter 1,$ parameter 2) when accessing a non-public/undefined method
__callstatic ($ parameter 1,$ parameter 2) when accessing a non-public/undefined static method
__isset () when using isset () for non-public/undefined properties
__unset () when using unset () for non-public/undefined properties

Note: These magic methods can play a very important role in writing the underlying application of the framework.

@zhnoah
Source: http://www.cnblogs.com/zhnoah/
This article is owned by me and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original
The right to pursue legal liability.

PHP Learning Note--10. Magic method

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.