Summary of common PHP magic methods

Source: Internet
Author: User
Common magic methods include __tostring () _ Call () _ autoLoad () _ clone () _ GET () _ SET () _ isset () _ unset () 1. _ Tostring () is used to define the reference of the output object. calling is often used to print the information of some objects. the returned value must exist. eg: Yes... common magic methods include __tostring () _ Call () _ autoLoad () _ clone () _ GET () _ SET () _ isset () _ unset ()
1. _ Tostring () is used to define the reference value of an output object. it is often called to print information of some objects and must return values.
Eg: there is a persion class
Persion per = new persion ()
Echo per; // an error occurs when calling directly.
We can add the _ tostring () method to the class definition.
Function _ Tostring ()
$ Str = this-> $ name. this-> age;
Return $ str;
2. _ clone () object replication
Reference assignment
$ Per1 = $ per2; this has only one address in the memory.
$ Per1 = clone $ per2 has two memory addresses.
3. the _ call () method is automatically executed when a function that does not exist in the class instance is called.
If you try to call a function that does not exist in the class, a syntax error occurs.
We can declare the Call () method in the class;
Function _ call ($ funName, $ argu)
Echo "the function named". $ funName. "parameter is". printf ($ argh). "does not exist ",
4. _ autoLoad: automatically loaded class files used. This function is added on the reference page.
We have used this case. to call other PHP files on the page, we need to use the include method.
However, if there are dozens of pages that need to be referenced, it is too cumbersome. we can use the autoload method in this page.
Function _ autoload ($ className)
Include $ className. ". php ";
In this way, all references to other classes will automatically reference this class of files. the prerequisite class file name must be class name. php
5. Private attributes in the _ GET () objects class
If a property in a class is set to a private property, it cannot be accessed in the class instance, but how can this be accessed?
We can use _ GET ()
Class person
Private $ name;
Private $ age;
Instantiate person per = new person ()
Per-> $ name; // The value cannot be obtained.
However, if we add the _ GET method to the class
Function _ GET ($ proName)
Return this-> $ proName;
We can call Per-> $ name again to access it.
In this case, some people may ask questions. what is the difference between directly accessing private variables and making them public?
If it is declared public, we can read it at will. if it is private, if we add the get method, the GET method will be called every time the private attribute is called, in this way, we can add some logic processing in the get method.
6. _ SET (): SET the private attribute in the class.
The principle is the same as above. we can add the _ SET () function to the class. every time a value is assigned to a private attribute by calling the class instance, the _ SET function is executed. the Function prototype is as follows:
Function _ SET ($ proName, $ value)
This-> $ proName = $ value;
Since it is a method assignment, we can do some logic processing.
7. _ isset () is automatically called when the private attribute or method in the class exists
First, we will first introduce the isset method, which is used to determine whether the attributes and methods exist, but we cannot determine whether a private attribute in the class exists through the class instance.
If we use isset (per-> $ name); // the returned value is false, but the $ name attribute does exist, how can this problem be solved?
1. Define $ name as a private property
Add in class definition
Function _ isset ($ proName)
Return isset (this-> $ proName); // you can access private attributes within the class.
In this case, we call isset ($ name) again; the return value is true;
8. _ unset () is automatically called when private variables in the class are cleared.
In combination, the unset () unset method can delete attributes. when we need to delete attributes in a class, if it is a public attribute, we can directly
Delete, but if it is private, we can't implement it by using this method.
How to implement it? we can use the _ unset () method to implement this function. we need to add
Function _ unset ($ proName)
Unset (this-> $ proName );
Now we can call unset ($ name); to delete the private attribute $ name in the person class.

From the column jt521xlg

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: 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.