Home > Backend Development > PHP Tutorial > Learn the magic method of PHP_PHP tutorial

Learn the magic method of PHP_PHP tutorial

WBOY
Release: 2016-07-21 14:52:25
Original
793 people have browsed it

__set_state()
var_export can export a collection as a string, which is some executable PHP code. In the object-oriented version of php5.1.0, a static method called __set_state is introduced to enable var_export to support the export of object instances. When using var_export to export an instance, the exported string contains the code that calls this static method. This method takes one parameter, which is an array containing all member properties of the exported instance. It's a bit abstract, look at the example below.
$v) { $obj->$k = $v; } return $obj; }}$i = new o;$i->age = 21;eval('$b = '.var_export($i,true).';');//true here means that var_export returns Export the string instead of printing it. print_r($b);/*Output: stdClass Object( [skill] => php [age] => 21)*/?>

__clone()
In PHP5, assignments between objects are always passed by address reference. For example, the following example will output 66 instead of 55.
age = 66;echo $i2->age;?>
If you want to pass it as an actual value, you need to use the clone keyword.
age = 66;echo $i2->age;//Output 55?>
However, only the $i instance is cloned here. If a member attribute of $i is also an instance, then this member attribute will still be passed to $i2 by reference. For example, the following example:
sub=new o2;$i2 = clone $ i;$i->sub->p=5;echo $i2->sub->p;?>
The final output is 5, not 1. In other words, although $i and $i2 do not point to the same instance, their member attributes $sub point to the same instance. At this time, we must use the __clone method to copy $sub. In the o class, add the __clone() method. As follows:
sub=clone $this->sub; }}//......? >
In this way, when echo $i2->sub->p;, the output is the value 1 when passing.
__autoload()
When creating an instance, if the corresponding class does not exist, __autoload() will be executed. This function has one parameter, which is the class name corresponding to the instance to be created. In the following example, when creating an instance of the test class, if /home/surfchen/project/auto.php exists, require this file, otherwise print a Class test Not Found error and abort the current script.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/371638.htmlTechArticle__set_state() var_export can export a set into a string of strings, which are some executable PHP code. In the object-oriented version of php5.1.0, a function called __set_stat...
is introduced
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