I found a strange behavior when passing parameter by reference to an object method:
class Test
{
private $value;
public function Set($value)
{
$this->value = $value;
}
public function Get(&$ref)
{
$ref = &$this->value; //SET REF PARAMETER TO THIS VALUE BY REF
}
}
$test = new Test();
$test->Set('test');
$test->Get($value1);
var_dump($value1); //NULL INSTEAD OF 'test'!
edit: GetByRef(...) name was wrong for this example, renamed to: Get(...)
edit2: I forgot the real test case where I stucked:
$test->Get($value1);
$test->Get($value2);
$value1 = 'Another test value';
echo $value2; //SHOULD BE SAME: 'Another test value';
$value2 does not know if value1 created or not, so the standard $value2 = &$value1 not works here.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…