Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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_Method Chaining - Fatal编程技术网

PHP:链中前一个方法的名称

PHP:链中前一个方法的名称,php,method-chaining,Php,Method Chaining,我是PHP中的方法链接,类似这样: $c = new someClass(); $c->blackMethod()->colourMethod(); $c->whiteMethod()->colourMethod(); $c->blackMethod()->colourMethod(); //output: "The colour is black"; $c->whiteMethod()->colourMethod(); //output:

我是PHP中的方法链接,类似这样:

$c = new someClass();

$c->blackMethod()->colourMethod();
$c->whiteMethod()->colourMethod();
$c->blackMethod()->colourMethod();
//output: "The colour is black";

$c->whiteMethod()->colourMethod();
//output: "The colour is white";
有没有办法让colorMethod()知道它是在blackMethod()还是whiteMethod()之后调用的?换句话说,有没有一种方法可以在方法链中获取先前调用的方法的名称

大概是这样的:

$c = new someClass();

$c->blackMethod()->colourMethod();
$c->whiteMethod()->colourMethod();
$c->blackMethod()->colourMethod();
//output: "The colour is black";

$c->whiteMethod()->colourMethod();
//output: "The colour is white";
我知道链接只是从同一个类调用多个方法的简写,但我希望有一种方法能够以某种方式将链接链接在一起

我已经尝试过debug_backtrace()和


但是它们只给出类名或调用colorMethod(即$c)的方法的名称,而不是在它之前调用的方法。

只需在对象上设置一个属性:

<?php

class ColorChanger
{
    private $lastColor;

    public function blackMethod() {
        echo "blackMethod(); Last color: {$this->lastColor}\n";
        $this->lastColor = 'black';
        return $this;
    }

    public function whiteMethod() {
        echo "whiteMethod(); Last color: {$this->lastColor}\n";
        $this->lastColor = 'white';
        return $this;
    }

    public function colourMethod() {
        echo "colourMethod(): {$this->lastColor}\n";
        $this->lastColor = null;
    }
}

$c = new ColorChanger();

$c->blackMethod()->colourMethod();
$c->whiteMethod()->colourMethod();

$c->blackMethod()->whiteMethod()->colourMethod();

只需在对象上设置一个属性:

<?php

class ColorChanger
{
    private $lastColor;

    public function blackMethod() {
        echo "blackMethod(); Last color: {$this->lastColor}\n";
        $this->lastColor = 'black';
        return $this;
    }

    public function whiteMethod() {
        echo "whiteMethod(); Last color: {$this->lastColor}\n";
        $this->lastColor = 'white';
        return $this;
    }

    public function colourMethod() {
        echo "colourMethod(): {$this->lastColor}\n";
        $this->lastColor = null;
    }
}

$c = new ColorChanger();

$c->blackMethod()->colourMethod();
$c->whiteMethod()->colourMethod();

$c->blackMethod()->whiteMethod()->colourMethod();

跟踪先前调用的方法的一种更好、更通用的方法是使用私有或受保护的属性,使用constante
\uuuuuuu函数或
\uuuu方法来跟踪实际的方法

例如:


跟踪以前调用的方法的一种更好、更通用的方法是使用constante
\uuuu函数或
\uuu方法
使用私有或受保护的属性跟踪实际的方法
例如:



blackMethod
应该在对象(属性等)上设置一些状态,然后
colorMethod
可以引用这些状态。无论你做什么,都不要使用回溯。我很好奇——你为什么需要回溯?我闻到一支枪指着你自己的脚:)同意,即使举个例子,它似乎也没有什么意义。你期望一个“颜色是……”的输出——但实际上为什么是这样呢?如果
blackMethod
whiteMethod
没有在任何地方设置任何颜色,那么任何颜色“在”哪里?CBroe,您提出了一个有趣的观点。我仍然很好奇是否有可能得到这个名字。一个类方法知道两件事——它的实例的状态和任何输入参数。您可以重构代码以停止使用流畅的接口(
$c->colorMethod('black')
),或者使用类的状态。
blackMethod
应该在对象(属性等)上设置一些状态,然后
colorMethod
可以引用这些状态。无论你做什么,都不要使用回溯。我很好奇——你为什么需要回溯?我闻到一支枪指着你自己的脚:)同意,即使举个例子,它似乎也没有什么意义。你期望一个“颜色是……”的输出——但实际上为什么是这样呢?如果
blackMethod
whiteMethod
没有在任何地方设置任何颜色,那么任何颜色“在”哪里?CBroe,您提出了一个有趣的观点。我仍然很好奇是否有可能得到这个名字。一个类方法知道两件事——它的实例的状态和任何输入参数。您可以重构代码以停止使用流畅的接口(
$c->colorMethod('black')
),或者使用类的状态。Franker,感谢您的及时回复。因此,在运行前一个方法时,如果不存储一个值,就无法“回头看”?@dearsina Check-out-example(不过,我不建议这样做,因为性能/失去了拥有公共/私有方法的能力,而没有更多的魔力)。最好的方法是在返回
$this
并链接到下一个方法之前,将前一个状态存储在类中的某个私有变量中。您可以创建一个简单的
$this->log()
函数来记录当前方法调用以及传递的任何参数。Franker,感谢您的及时回复。因此,在运行前一个方法时,如果不存储一个值,就无法“回头看”?@dearsina Check-out-example(不过,我不建议这样做,因为性能/失去了拥有公共/私有方法的能力,而没有更多的魔力)。最好的方法是在返回
$this
并链接到下一个方法之前,将前一个状态存储在类中的某个私有变量中。您可以创建一个简单的
$this->log()
函数,用于记录当前方法调用以及传递的任何参数。这实际上仍然是通过每个方法存储状态。他回复了我的评论:“因此,在运行前一个方法时,如果不存储一个值,就无法“回头看”?”,你的答案似乎有点漏洞百出保持跟踪的唯一方法是有效地保存当前状态,我不否认。但我的建议是使用适当的常量自动跟踪当前方法名称,而不是每次手动编写。我认为它更通用,可以用于任何项目。我真的希望有一种方法可以避免手动跟踪,但如果这是唯一的方法,那么您的通用方法可能是实现它的最简单方法。谢谢你们两位的帮助!这实际上仍然是通过每个方法存储状态。他回复了我的评论:“因此,在运行前一个方法时,如果不存储一个值,就无法“回头看”?”,你的答案似乎有点漏洞百出保持跟踪的唯一方法是有效地保存当前状态,我不否认。但我的建议是使用适当的常量自动跟踪当前方法名称,而不是每次手动编写。我认为它更通用,可以用于任何项目。我真的希望有一种方法可以避免手动跟踪,但如果这是唯一的方法,那么您的通用方法可能是实现它的最简单方法。谢谢你们两位的帮助!