Common magic Methods for PHP

Source: Internet
Author: User
I have compiled some of the PHP magic methods that will be used frequently in the development process, hoping to be helpful to everyone.

The Magic method is a predefined function in PHP that has a special purpose (which is triggered under certain conditions), which can be said to be a syntax conversion convention;

Magic methods are started by double underscores, such as __construct (), __destruct (), etc.;


Common Methods of Magic:

__construct,__destruct,__get,__set,__tostring


__construct:

Function: Initializes the object of the class

Called: Called when creating an object of a class

Parameters: Custom

__destruct:

Role: Destroying objects of a class

Call: When all references to the object of the class are not valid, the invoked condition is met, and the call of the PHP parser waits

Parameters: Object Name


Protection mechanisms

__call:

Role: Protects when a member method that does not exist is called

Called: Called when a member method that does not exist is called

Parameters: Function name, parameter class table (enumerated array)

__callstatic

Function: Protects when a static member method that does not exist is called

Call: Called when a static member method that does not exist is called

Parameters: Function name, parameter list (enum array)

__get:

Function: Protects when a nonexistent property is acquired

Called: Called when attempting to obtain a property that does not exist

Parameters: Variable Name

__set:

Function: Protects a non-existent attribute when assigned a value

Call: When attempting to assign a value to a property that does not exist

Parameters: variable name, value

__tostring:

Function: How a Class (object) should respond when treated as a string

Called: Called when an attempt is being taken to output a class object.

These are some of the common magic methods that I've compiled for PHP, and you can add some other methods by commenting.

Related articles:

PHP implementation of MySQL class with read and write separation function

PHP Strpos () function introduction and how to use a detailed explanation

Phpecho function Introduction and use 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.