Home >Backend Development >PHP Tutorial >Summary of commonly used magic methods in php
In object-oriented programming, PHP provides a series of magic methods, which provide a lot of convenience for programming. Magic methods in PHP usually start with (two underscores) and do not require explicit calls but are triggered by certain conditions. This article only introduces the commonly used magic methods in php. Friends who need it can come and refer to it
Commonly used magic methods are: Tostring () Call() autoLoad() clone () GET() SET() isset() unset()
1.Tostring() Used to define the output object Called when referencing Commonly used Printing information about some objects must have a return value
eg: There is a persistence class
Persion per =new persion()
Echo per; //Direct call will cause an error
We can add it in the definition of the class Add tostring() method
The code is as follows:
Function Tostring() { $str=this->$name.this->age; Return $str; }
2.Clone() object copy
Reference assignment
$per1=$ per2; And there is only one address in the memory
And $per1=clone $per2 At this time there are two memory addresses
3. The call() method is used when calling the class instance that does not exist Function is automatically executed
If you try to call a function that does not exist in the class, a syntax error will appear. In order to provide a friendly prompt
we can declare the Call() method in the class;
Function call($funName,$argu) { Echo "名为".$funName."参数为".printf($argh)."的函数不存在", }
4.autoLoad Automatic loadingClass files used This function is added to the referenced page
We have all used this situation, and it is needed in the page To call other php files, we need to use the include method
. But if there are dozens of pages that need to be referenced, it is too cumbersome. We can use the autoload method
Function autoload($className) { Include $className.".php"; }
on the page. In this way, wherever other classes are referenced, the class file will be automatically referenced. The name of the prerequisite class file must be Class name.php
5.GET() Access private properties in the class
If the attributes in the class are set to private attributes, they are inaccessible in instances of the class, but how can they be accessed?
We can use GET()
Eg:
There is
Class person { Private $name; Private $age; }
in the class to instantiate person per=new person()
Per-> $name; //The value cannot be obtained in this way
But if we add the GET method in the class
Function GET($proName) { Return this->$proName; }
We can access it by calling Per->$name again
Someone can do this You may ask, what is the difference between directly accessing private variables and declaring them public?
If it is declared public, we can read it arbitrarily. If it is private, if we add the get method, then the content of the GET method will be called every time the private attribute is called, so we can add some logic to the get method. deal with.
6.SET() sets private properties in the class
The principle is the same as above. We can add the SET() function to the class. Whenever the class instance is called, When assigning private properties, the SET function will be executed. Function prototype:
Function SET($proName,$value) { This->$proName=$value; }
Since it is a method assignment, we can do some logical processing
7.isset( ) Automatically call
when determining whether private properties or methods in a class exist. First, let’s introduce the isset method. This method is used to determine whether properties and methods exist, but we cannot pass the class Instance determines whether a private attribute in the class exists
If we use isset(per->$name);//The return value is false, but the $name attribute does exist, how to solve the problem?
Solution:
1. Define $name as a private property
2. Add
Function isset($proName) { Return isset(this->$proName);//再类内部是可以访问私有属性的 }in the class definition
In this case, we call isset($name) again; the return value will be true;
8.unset() is automatically called when clearing private variables in the class
Combined with it is unset(). The unset method can delete attributes. When we need to delete attributes in a class, if it is a public attribute, we can directly
delete it, but if it is private, we cannot achieve it only through this method.
How to implement it? We can use the unset() method to achieve this function. We need to add
Function unset($proName) { Unset(this->$proName); }
Now we call unset($name); to delete the private attributes in the person class$ Name
The above is the detailed content of Summary of commonly used magic methods in php. For more information, please follow other related articles on the PHP Chinese website!