douqie3391 2014-08-12 16:54
浏览 15
已采纳

如何使用引用获取最后的方法输出?

I have this difficult problem, which may be solved using references. I have tried to put this & reference symbol, just about everywhere, albeit to no avail.

Here is a simplified script to demonstrate my base application.

<?php 
class main
{
    private $property = []; 

    function a()
    {
        $this->property[] = 'method: '.__METHOD__.' was called '; 
        return $this; 
    }

    function b()
    {
        $this->property[] = 'method: '.__METHOD__.' was called '; 
        return $this; 
    }

    function c()
    {
        $this->property[] = 'method: '.__METHOD__.' was called '; 
        return $this; 
    }

    function end()
    {
        var_dump($this->property);
    }
}

As you can see, it is a simple class with its methods, all adding value to one class property, all the methods return the class object (are chainable), except for the end() method.

Now, for the purpose of my application, I have to make calls to the class methods

$a = new main;
$a->a()->end(); 
$a->b()->end(); 
$a->c()->end(); 

Now, the problem as you can see, the output will be something like this.

array(1) {
  [0]=>
  string(27) "method: main::a was called "
}
array(2) {
  [0]=>
  string(27) "method: main::a was called "
  [1]=>
  string(27) "method: main::b was called "
}
array(3) {
  [0]=>
  string(27) "method: main::a was called "
  [1]=>
  string(27) "method: main::b was called "
  [2]=>
  string(27) "method: main::c was called "
}

What I am looking for is, to only get the last array. that is:

array(3) {
  [0]=>
  string(27) "method: main::a was called "
  [1]=>
  string(27) "method: main::b was called "
  [2]=>
  string(27) "method: main::c was called "
}

Because, as shown in my previous code, I am calling the function this way.

$a = new main;
$a->a()->end(); 
$a->b()->end(); 
$a->c()->end(); 

And it makes sense to get the last array, instead of the other two. I realize, one way to achieve this would be, to start the object three time, as in

(new main)->a()->end(); 
(new main)->b()->end(); 
(new main)->c()->end(); 

But, I am hoping, that somewhere in-between, using clone or reference, it might be possible to get only the last array.

Thanks

  • 写回答

1条回答 默认 最新

  • dongwo2222 2014-08-12 16:57
    关注

    How about this?

    <?php 
    class main
    {
        private $property = []; 
        private $outputs  = 0;
    
        function a()
        {
            $this->property[$this->outputs][] = 'method: '.__METHOD__.' was called '; 
            return $this; 
        }
    
        function b()
        {
            $this->property[$this->outputs][] = 'method: '.__METHOD__.' was called '; 
            return $this; 
        }
    
        function c()
        {
            $this->property[$this->outputs][] = 'method: '.__METHOD__.' was called '; 
            return $this; 
        }
    
        function end()
        {
            var_dump($this->property);
            $this->outputs++;
        }
    }
    

    It won't give you the last array, but you can get it from the output if you change the end method:

        function end()
        {
            var_dump($this->property[$this->outputs]);
            $this->outputs++;
        }
    

    If you want only 1 array with last call I'm with @Adherence, it's quite impossible without analising your code which complicates (a lot!) this thing...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP