Home > Article > Backend Development > PHP reference operation and methods of local static variables of external operation functions_PHP tutorial
Static variables inside functions or member methods are manipulated externally by reference
The following is a simple example to illustrate three issues related to citation:
1. Type conversion within the function after parameter reference is also an address operation
2. When passing parameters to other functions after quoting, you need to add the reference character again to maintain the address operation
3. The function return value reference must be added with the reference operator when the function is declared and called
This example uses the operation of object method, and the same applies to functions
<?php class A { public function & test1(& $a) { static $i = 0; var_dump($i); $a = (array) $a; $a['domain'] = 'http://blog.csdn.net/zhouzme'; $this->test2($a); $i++; var_dump($a); var_dump($i); return $i; } protected function test2(& $a) { $a['name'] = '蜗牛'; } } $obj = new A(); $a = ''; $c = & $obj->test1($a); $c++; var_dump($a); $obj->test1($a); var_dump($a);
Output results