function test() {$a = 1;$b = 2;testa( 'testb', $a );echo $a, $b;}function testa() {$p = func_get_args();$fun = $p[ 0 ];$p1 = & $p[ 1 ]; $fun( $p1 );}function testb( &$a, &$b ) {$a = 'a';$b = 'b';}test();
1 回答

qq_笑_17
TA貢獻1818條經驗 獲得超7個贊
我依然不是太明白你的表達 ...
如果你是想通過 func_get_args() 來獲取一個參數變量的引用 ... 很遺憾 ... 你做不到 ...
不過我們可以用一些替代方案來完成 ... 沒細去琢磨 ... 第一時間能想到的方法類似下面這樣 ...
<?phpfunction test() { /* make an object and forget about reference ... */ $sunyanzi = (object)[ 'a' => 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();
不太喜歡你的代碼風格所以小修改了一下 ... 但愿不會影響恩 ...
這種方式雖然可以實現 ... 但是從架構的角度講不是太好 ...
因為在對象傳遞的過程中 ... 你無法取消這個引用 ... 所以盡量還是換一種程序結構吧 ...
恩 ... 就是這樣啦 ... 希望我沒誤會你的意思 ...
- 1 回答
- 0 關注
- 124 瀏覽
添加回答
舉報
0/150
提交
取消