PHP:链中上一个方法的名称
PHP: The name of the previous method in chain
我在 PHP 中进行方法链接,如下所示:
$c = new someClass();
$c->blackMethod()->colourMethod();
$c->whiteMethod()->colourMethod();
colourMethod() 有什么方法可以知道它是在 blackMethod() 还是 whiteMethod() 之后调用的?换句话说,有没有办法获取方法链中之前调用的方法的名称?
像这样:
$c->blackMethod()->colourMethod();
//output: "The colour is black";
$c->whiteMethod()->colourMethod();
//output: "The colour is white";
我知道链接只是 shorthand 用于从同一个 class 调用多个方法,但我希望有一种方法可以 link 以某种方式将链接在一起。
我试过 debug_backtrace() 和
$e = new Exception();
$trace = $e->getTrace();
但他们只给出 class 名称或调用 colourMethod 的方法的名称($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();
例子here.
如果您需要获取历史记录,use an array:
<?php
class ColorChanger
{
public $lastColors = [];
public function blackMethod() {
$colors = implode(', ', $this->lastColors);
echo "blackMethod(); Last colors: {$colors}\n";
$this->lastColors[] = 'black';
return $this;
}
public function whiteMethod() {
$colors = implode(', ', $this->lastColors);
echo "whiteMethod(); Last colors: {$colors}\n";
$this->lastColors[] = 'white';
return $this;
}
public function colourMethod() {
$colors = implode(', ', $this->lastColors);
$lastColor = $this->lastColors[count($this->lastColors)-1];
echo "colourMethod(): {$colors} (Last: $lastColor)\n";
$this->lastColors = [];
}
}
$c = new ColorChanger();
$c->blackMethod()->colourMethod();
$c->whiteMethod()->colourMethod();
$c->blackMethod()->whiteMethod()->blackMethod()->colourMethod();
跟踪先前调用的方法的一种更好且更通用的方法是使用私有或受保护的 属性 来跟踪使用常量 __FUNCTION__
或 __METHOD__
示例:
class colorchanger
{
protected prevMethod;
public function black()
{
/*
your routine
for the black
method here
*/
$this->prevMethod =__FUNCTION__;
return $this;
}
public function white()
{
/*
your routine
for the white
method here
*/
$this->prevMethod =__FUNCTION__;
return $this;
}
public function colour()
{
return $this->prevMethod;
}
}
$c = new ColorChanger();
$c->black->colour();
$c->white()->colour();
$c->black()->white->colourMethod();//will return white instead of black
我在 PHP 中进行方法链接,如下所示:
$c = new someClass();
$c->blackMethod()->colourMethod();
$c->whiteMethod()->colourMethod();
colourMethod() 有什么方法可以知道它是在 blackMethod() 还是 whiteMethod() 之后调用的?换句话说,有没有办法获取方法链中之前调用的方法的名称?
像这样:
$c->blackMethod()->colourMethod();
//output: "The colour is black";
$c->whiteMethod()->colourMethod();
//output: "The colour is white";
我知道链接只是 shorthand 用于从同一个 class 调用多个方法,但我希望有一种方法可以 link 以某种方式将链接在一起。
我试过 debug_backtrace() 和
$e = new Exception();
$trace = $e->getTrace();
但他们只给出 class 名称或调用 colourMethod 的方法的名称($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();
例子here.
如果您需要获取历史记录,use an array:
<?php
class ColorChanger
{
public $lastColors = [];
public function blackMethod() {
$colors = implode(', ', $this->lastColors);
echo "blackMethod(); Last colors: {$colors}\n";
$this->lastColors[] = 'black';
return $this;
}
public function whiteMethod() {
$colors = implode(', ', $this->lastColors);
echo "whiteMethod(); Last colors: {$colors}\n";
$this->lastColors[] = 'white';
return $this;
}
public function colourMethod() {
$colors = implode(', ', $this->lastColors);
$lastColor = $this->lastColors[count($this->lastColors)-1];
echo "colourMethod(): {$colors} (Last: $lastColor)\n";
$this->lastColors = [];
}
}
$c = new ColorChanger();
$c->blackMethod()->colourMethod();
$c->whiteMethod()->colourMethod();
$c->blackMethod()->whiteMethod()->blackMethod()->colourMethod();
跟踪先前调用的方法的一种更好且更通用的方法是使用私有或受保护的 属性 来跟踪使用常量 __FUNCTION__
或 __METHOD__
示例:
class colorchanger
{
protected prevMethod;
public function black()
{
/*
your routine
for the black
method here
*/
$this->prevMethod =__FUNCTION__;
return $this;
}
public function white()
{
/*
your routine
for the white
method here
*/
$this->prevMethod =__FUNCTION__;
return $this;
}
public function colour()
{
return $this->prevMethod;
}
}
$c = new ColorChanger();
$c->black->colour();
$c->white()->colour();
$c->black()->white->colourMethod();//will return white instead of black