• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php 引用 问题

php 搞代码 3年前 (2022-01-25) 9次浏览 已收录 0个评论
文章目录[隐藏]

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() 来获取一个参数变量的引用 … 很遗憾 … 你做不到 …

不过我们可以用一些替代方案来完成 … 没细去琢磨 … 第一时间能想到的方法类似下面这样 …

<?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 $sun<div style="color:transparent">!本文来源gaodai.ma#com搞#代!码(网</div><em>搞gaodaima代码</em>yanzi->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();

不太喜欢你的代码风格所以小修改了一下 … 但愿不会影响恩 …

这种方式虽然可以实现 … 但是从架构的角度讲不是太好 …

因为在对象传递的过程中 … 你无法取消这个引用 … 所以尽量还是换一种程序结构吧 …

恩 … 就是这样啦 … 希望我没误会你的意思 …


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php 引用 问题

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址