dongyi1982 2015-10-20 07:34 采纳率: 0%
浏览 11

php类返回; 功能回归

object var $c->$var1 has been changed in class a;

var $new_var has not be changed in function d; i was a litte puzzled. my english is poor,can you understand me?

<?php
    class a{
        public function test () {
            $b = new b;
            $c = new c;
            $b->test($c);
            var_dump($c);
        } 
    }

    class b{
        public function test($c) {
            $c->var1 = 2;
            return $c;
        }
    }

    class c {
        public $var1 = 1;
    }

    $a = new a;
    $a->test();

    function d($new_var) {
        $new_var = 2;
        return $new_var;
    }

    $new_var = 1;
    d($new_var);
    echo $new_var
  • 写回答

2条回答 默认 最新

  • dsaeyrq451928 2015-10-20 07:45
    关注

    You problem is that you have this function

    function d($new_var) {
            $new_var = 2;
            return $new_var;
    }
    

    Where is it true that you maybe pass a different variable as parameter, but then you take that parameter and you decide to set it = 2 and then return it. If you want to return what you passed you should change it in

    function d($new_var) {
            return $new_var;
    }
    

    Or if you want something similar to test function of class b try tgus

    function d($new_var) {
        $d->new_var = $new_var;
        return $d;
    }
    

    So you can access to your $new_var in the returned objecd $d

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么