2013-03-09 2 views
0

필자는 PHP의 "pass by reference"에 대한 혼란이 나에게 영향을 미쳤다는 것을 인정해야한다.프라이빗 어레이에 대한 참조 전달

class TestClass { 

    private $my_precious = array ('one','two','three'); 

    public function &give_reference() { 
     return $this->my_precious; 
    } 

} 

$foobar = new TestClass(); 
$my_ref = $foobar->give_reference(); 
$my_ref = array ("four", "five", "six"); 

echo print_r($foobar,true); 

인쇄 것이다 : 나는 다음과 같은 코드를 생각했을 것이다

TestClass Object 
(
    [my_precious:TestClass:private] => Array 
     (
      [0] => four 
      [1] => five 
      [2] => six 
     ) 

) 

을하지만, 슬프게도, 내 참조에 더 지구력이없는 것 같다 그것은 대신의 반향 :

TestClass Object 
(
    [my_precious:TestClass:private] => Array 
     (
      [0] => one 
      [1] => two 
      [2] => three 
     ) 

) 

어떻게 이 일을 할 수 있을까요?

답변

2

당신은뿐만 아니라 참조로 할당 할 수 있습니다

$my_ref =& $foobar->give_reference(); 
0

시도 :

class TestClass { 

    private $my_precious = array ('one','two','three'); 

    public function & give_reference() { 
     return $this->my_precious; 
    } 

} 

$foobar = new TestClass(); 
$my_ref = & $foobar->give_reference(); 
$my_ref = array ("four", "five", "six"); 

echo print_r($foobar,true);