Home > Backend Development > PHP Tutorial > Summary of common magic methods in php_PHP tutorial

Summary of common magic methods in php_PHP tutorial

WBOY
Release: 2016-07-21 15:00:11
Original
817 people have browsed it

Commonly used magic methods are: __Tostring () __Call() __autoLoad() __ clone() __GET() __SET() __isset() __unset()

1 .__Tostring() Used to call when defining an output object reference. Often used to print information about some objects that must have a return value
eg: There is a persion class
Persion per =new persion()
Echo per ; //Direct calling will cause an error
We can add the __tostring() method in the class definition

Copy the code The code is as follows:

Function __Tostring()
{
$str=this->$name.this->age;
Return $str;
}

2. Copy of __clone() object
Reference assignment
$per1=$per2; And this has only one address in the memory
And $per1=clone $per2 At this time There are two memory addresses

3.__call() method is automatically executed when calling a function that does not exist in the class instance
If you try to call a function that does not exist in the class, it will appear Syntax error, in order to provide a friendly prompt
we can declare the Call() method in the class;
Copy the code The code is as follows:

Function __call($funName,$argu)
{
Echo "The function named ".$funName." with the parameter ".printf($argh)." does not exist",
}

4.__autoLoad automatically loads the class files used. This function is to add
to the referenced page. We have all used this situation and need to call other php in the page. file, we need to use the include method
but if there are dozens of pages that need to be referenced, it will be too cumbersome. We can use the autoload method
Copy code The code is as follows:

Function __autoload($className)
{
Include $className.".php";
}

In this way, wherever other classes are referenced, the class file will be automatically referenced. The prerequisite class file name must be class name.php

5.__GET() Access private attributes in the class
If the attributes in the class are set to private attributes, they are inaccessible in the instance of the class, but how can they be accessed?
We can use __GET()
Eg:
class has

Copy code The code is as follows :

Class person
{
Private $name;
Private $age;
}

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
Copy code The code is as follows:

Function __GET($proName)
{
Return this->$proName;
}

We can access it by calling Per->$name again
Some people may raise questions by doing this. In this way, private variables can be accessed directly. What is the difference between 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 attributes in the class
The principle is the same as above. We can add the __SET() function to the class. Whenever a private attribute is assigned a value by calling a class instance, it will Execute the __SET function, function prototype:

Copy code The code is as follows:

Function __SET($ proName,$value)
{
This->$proName=$value;
}

Since it is a method assignment, we can do some logical processing

7.__isset() is automatically called when determining whether private attributes or methods in a class exist
First of all, let’s introduce the isset method, which is used to determine whether attributes and methods exist, but we It is impossible to determine whether a private attribute in the class exists through the class instance
If we use isset(per->$name);//The return value is false, but the $name attribute does exist, how to solve it?
Solution:
1. Define $name as a private property
2. Add

Copy code The code is as follows:
Function __isset($proName)
{
Return isset(this->$proName ); //Private properties can be accessed inside the class
}


In this case, we call isset($name) again; the return value will be true;

8.__unset() is automatically called when clearing private variables in a class
Combined with it is unset(). The unset method can delete attributes. When we need to delete attributes in a class, if If it is a public attribute, we can directly
delete it, but if it is private, we cannot achieve it through this method alone.
How to implement it? We can use the __unset() method to achieve this function. We need to add
to the class.

Copy code The code is as follows:

Function __unset($proName)
{
Unset(this->$proName );
}

Now we call unset($name); to delete the private attribute $name in the person class

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328095.htmlTechArticleCommonly used magic methods are: __Tostring () __Call() __autoLoad() __clone() __GET() __SET( ) __isset() __unset() 1.__Tostring() is used to call when defining an output object reference. It is often used to print a...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template