Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用引用获取最后的方法输出?_Php_Oop_Method Chaining - Fatal编程技术网

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

Php 如何使用引用获取最后的方法输出?,php,oop,method-chaining,Php,Oop,Method Chaining,我有一个难题,可以用参考资料来解决。我试着把这个&参考符号放在几乎所有的地方,尽管没有用 下面是一个简化的脚本来演示我的基本应用程序 <?php class main { private $property = []; function a() { $this->property[] = 'method: '.__METHOD__.' was called '; return $this; } fun

我有一个难题,可以用参考资料来解决。我试着把这个
&
参考符号放在几乎所有的地方,尽管没有用

下面是一个简化的脚本来演示我的基本应用程序

<?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);
    }
}
现在,问题是这样的

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 "
}
我要找的是,只获取最后一个数组。即:

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 "
}
因为,如我前面的代码所示,我是这样调用函数的

$a = new main;
$a->a()->end(); 
$a->b()->end(); 
$a->c()->end(); 
获取最后一个数组而不是另外两个数组是有意义的。我意识到,实现这一点的一种方法是,启动对象三次,如

(new main)->a()->end(); 
(new main)->b()->end(); 
(new main)->c()->end(); 
但是,我希望,在这两者之间的某个地方,使用
clone
reference
,可能只获得最后一个数组

谢谢

这个怎么样

<?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++;
    }
}

如果您只需要一个数组来进行最后一次调用,我使用@attraction,那么不分析您的代码是不可能的,这会使这件事变得复杂(很多!)。

在最后一次调用时只调用end()而不是调用a和b如何?是的,这样就可以了。但是,每个方法都必须有自己的
end()
。这可能没有多大意义,因为这是一个简化的例子。但是,我的应用程序需要和
end()。end()方法无法知道最后一次调用它的时间。因此,它不知道何时输出最终数组,您可以获取执行PHP的文件的内容,并检查最后一个端点是否与调用它的类对应。严肃地说,是时候重新考虑你的策略了。为什么需要这个?这提供了三种不同的阵列。一个数组中没有三个值。好吧,很抱歉回答得太快了,现在呢?事实上,现在我想起来了。你的第一个答案改进了我的应用程序。有时我们只是错过了一些简单的事情:-)
<?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++;
    }
}
    function end()
    {
        var_dump($this->property[$this->outputs]);
        $this->outputs++;
    }