The following is a common magic method in PHP in a detailed summary of the introduction, the need for friends can come to the reference
Common magic Methods are: __tostring () __call () __autoload () __ Clone () __get () __set () __isset () __unset ()
1.__tostring () is used to define output object references when calling information that is commonly used to print some objects must have a return value
Eg: there is a persion class
Persion per =new persion ()
Echo per; Direct call will go wrong
We can add the __tostring () method to the definition of the class
Function __tostring () {$str=this->$name.this-> age; Return $str ;}
replication of 2.__clone () objects
Reference assignment
$per 1= $per 2; And this has only one address in memory.
and $per1=clone $per 2 with two memory addresses.
the 3.__call () method executes automatically when a function that does not exist in the class instance is called
If you try to invoke a function that does not exist in the class, a syntax error occurs, in order to be friendly hints
We can declare the call () method in the class;
Function __call ($funName,$argu) {Echo "is named". $funName. " Parameter is ". printf ($argh). " function does not exist ",}
4.__autoload automatically loads the class file used by the function is added on the referenced page
We have used this situation, we need 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, any reference to other classes will automatically refer to the class file. The name of the prerequisite class file must be the class name. php
5.__get () Accessing private properties in a class
If a property in a class is set to a private property, it is inaccessible in an instance of the class, but how can it be accessed?
We can use __get ()
Eg:
class that has
Class person{Private$name; Private $age ;}
Instantiate person Per=new person ()
Per-> $name; This is not a value.
But if we add the __get method to the class
Function __get ($proName) {Return this->$proName;}
We call Per-> again $name we have access to the
This will ask questions, so that you can directly access the private variables, and declared as public what is the difference?
If it is declared as public, we can read it arbitrarily, and if it is private, if we add a Get method, the contents of the Get method will be called each time the private property is called, so that we can add some logic to the GET method.
6.__set () Setting private properties in a class
In the same principle, we can add the __set () function to the class, and every time we assign a value to a private property by invoking the class instance, we execute the __set function, the function prototype:
Function __set ($proName,$value) {This--$proName=$value ;}
Since it is a method assignment, we can do some logical processing
7.__isset () automatically called when a private property or method exists in a class
First we introduce the Isset method, which is used to determine whether a property or method exists, but we cannot judge whether a private property exists in a class through a class class instance.
If we use Isset (per-> $name);//The return value is false, but the $name property does exist, how to solve it?
Workaround:
1. Define $name as a private property
2. Adding in the class definition
Function __isset ($proName) {Return isset(this->$proName ); // inside a class, you can access the private property. }
In this case we call Isset again ($name); The return value is true;
8.__unset () automatically called when a private variable in a class is cleared
In combination with the unset () unset method can delete the attribute, when we need to delete the property in the class, if it is a public property we can directly
Delete, but if it's private, we can't do it by this method.
How do we do that? We can use the __unset () method to implement this function we need to add the
Function __unset ($proName) {unset(this->$proName) ;}
Now we call unset ($name) and we can delete the private attribute in the person class $name
Summary of common magic methods in PHP