function add_some_extra($args)
{
$args['b']= "bbb";
}
$arr['a']= "aaa";
add_some_extra($arr);
fb::log($arr);
>>>>>>>>>> output>>>>>>>>>>>>>>
array(
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'a'=>'aaa',
)引用传递参数--------------------------------------------------------------------------
function add_some_extra(&$args)
{
$args['b']= "bbb";
}
$arr['a']= "aaa";
add_some_extra($arr);
fb::log($arr);
>>>>>>>>>> output>>>>>>>>>>>>>>
array(
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
'a'=>'aaa',
'b'=>'bbb'
)默认参数(参数为object时, 碉堡了。。。)----------------------------------------
function add_some_extra($args)
{
$args->b ="bbb";
}
$obj->a = "aaa";
add_some_extra($arr);
fb::log($arr);
>>>>>>>>>> output>>>>>>>>>>>>>>
stdClass(
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
a:'aaa',
b:'bbb'
)这个aObj会跟着变:
function aObj(&$a, &$b)
{
$a = $b;
}
$a = new StdClass;
$a->i = 1;
$b = new StdClass;
$b->i = 2;
aObj($a, $b);
$b->i = 3;
print $a->i;
这个aObj一点都不变,还是i=1:
方程式aObj() 跟命令行_command_not_changed_相当:
function aObj($a, $b)
{
$a = $b;
}
$a = new StdClass;
$a->i = 1;
$b = new StdClass;
$b->i = 2;
aObj($a, $b);
$b->i = 3;
print $a->i;
这个很详细的描述:
class testRefer{
public $obj ;
public function __construct(){
$obj = new StdClass;
}
public function test($a, $b, &$aRef, &$bRef)
{
//$a = $b; //$aOjb won't be changeed
//$a->i = 10; //$aOjb will be changeed to ->i=10
//$a->i = $b->i; //$aOjb will be changeed to ->i=2
//$a = clone $b; //$aOjb won't be changeed
//$aRef = $bRef; //$aOjb will always be changing follow bOjb
//$aRef = $b; //$aOjb will always be changing follow bOjb
//$aRef->i = 10; //$aOjb will be changeed to ->i=10
//$aRef->i = $bRef->i; //$aOjb will be changeed to ->i=2
//$aRef = clone $bRef; //$aOjb will be changeed to ->i=2
//$a = $bRef; //$aOjb won't be changeed
$this->obj = $b; // $testRefer->obj will always be changing follow bOjb
//$this->obj = $bRef; // $testRefer->obj will always be changing follow bOjb
}
}
$aOjb = new StdClass;
$aOjb->i = 1;
$bojb = new StdClass;
$bojb->i = 2;
$testRefer = new testRefer();
$testRefer->test($aOjb, $bojb,$aOjb, $bojb);
$bojb->i = 3;
var_dump($aOjb);
print '
';
var_dump($bojb);
print '
';
var_dump($testRefer->obj);
print '
';
普通变量测试-------------------------------------------------------------------
function a(&$a, &$b)
{
$a =& $b;
}
$a = 1;
$b = 2;
a($a, $b);
$b = 3;
print $a;
这里输出 a=1
function a2(&$a, &$b)
{
$a = $b;
}
$a = 1;
$b = 2;
a2($a, $b);
$b = 3;
print $a;
这里输出 a=2
方程式a() 跟以下命令行_command_not_changed_相当:
_command_not_changed_:
//a=1
$a = 1;
$b = 2;
$a1 =& $a;
$b1 =& $b;
$a1 = &$b1; //指向被改了
$b = 3;
这里输出 a=1
------------------------
//这个才会跟着变:
$a = 1;
$b = 2;
$a =& $b;
$b = 3;
print $a;
这里输出 a=1