function test() {
$a = 1;
$b = 2;
testa( 'testb', $a );
echo $a, $b;
}
function testa() {
$p = func_get_args();
$fun = $p[ 0 ];
$p1 = & $p[ 1 ]; // 如何將 $p[ 1 ] 用傳址方式 傳給 $p[ 0 ]
$fun( $p1 );
}
function testb( &$a, &$b ) {
$a = 'a';
$b = 'b';
}
test();
回複內容:
function test() {
$a = 1;
$b = 2;
testa( 'testb', $a );
echo $a, $b;
}
function testa() {
$p = func_get_args();
$fun = $p[ 0 ];
$p1 = & $p[ 1 ]; // 如何將 $p[ 1 ] 用傳址方式 傳給 $p[ 0 ]
$fun( $p1 );
}
function testb( &$a, &$b ) {
$a = 'a';
$b = 'b';
}
test();
我依然不是太明白你的表達 ...
如果你是想通過 func_get_args() 來擷取一個參數變數的引用 ... 很遺憾 ... 你做不到 ...
不過我們可以用一些替代方案來完成 ... 沒細去琢磨 ... 第一時間能想到的方法類似下面這樣 ...
1, 'b' => 2 ]; /* just call the function ... */ func_caller( 'callee', $sunyanzi ); /* is this the result you want ..? */ echo $sunyanzi->a, $sunyanzi->b; /* done ... */ return;}function func_caller() { /* you can not get reference via func_get_args() ... */ $args = func_get_args(); /* using the most normal way to call the function ... */ return $args[0]( $args[1] );}function callee( $object ) { /* a different way to assign value ... */ $object->a = 'a'; $object->b = 'b'; /* actually i just replace "$" into "$object->" ... */ return;}/* here we go ... */test();
不太喜歡你的代碼風格所以小修改了一下 ... 但願不會影響恩 ...
這種方式雖然可以實現 ... 但是從架構的角度講不是太好 ...
因為在對象傳遞的過程中 ... 你無法取消這個引用 ... 所以盡量還是換一種程式結構吧 ...
恩 ... 就是這樣啦 ... 希望我沒誤會你的意思 ...