PHP OOP链接方法
我有以下代码:PHP OOP链接方法,php,method-chaining,Php,Method Chaining,我有以下代码: class one{ public $instance; function instance(){ $this->instance = 'instance was created'; } function execute(){ $this->instance .= "and something happened"; } } $class = new one; $class->inst
class one{
public $instance;
function instance(){
$this->instance = 'instance was created';
}
function execute(){
$this->instance .= "and something happened";
}
}
$class = new one;
$class->instance();
$class->execute();
echo $class->instance;
它做了我期望它做的事情,但我如何链接动作,例如,我如何在一行中调用这些函数:
$class->instance()->execute();
我知道这样做是可能的:
one::instance()->execute();
但在这种情况下,我需要使用使事情变得复杂的静态函数,我需要对这些事情进行一些解释为了使链接起作用,您需要从每个要链接的方法返回
$this
:
class one{
public $instance;
function instance(){
$this->instance = 'instance was created';
return $this;
}
function execute(){
$this->instance .= "and something happened";
return $this;
}
}
另外,给属性起与方法相同的名字是个坏主意。对于解析器来说,它可能是明确的,但是对于开发人员来说,它是混乱的。为了使链接起作用,您需要从您想要链接的每个方法返回
$this
:
class one{
public $instance;
function instance(){
$this->instance = 'instance was created';
return $this;
}
function execute(){
$this->instance .= "and something happened";
return $this;
}
}
另外,给属性起与方法相同的名字是个坏主意。对于解析器来说,它可能是明确的,但是对于开发人员来说,它是混乱的。您需要在函数末尾返回实例:
class one{
public $instance;
function instance(){
$this->instance = 'instance was created';
return $this;
}
function execute(){
$this->instance .= "and something happened";
return $this;
}
}
然后你可以把它们锁起来
顺便说一下,这可能只是示例代码,但您的
实例函数实际上并没有创建实例;) 您需要在函数结束时返回实例:
class one{
public $instance;
function instance(){
$this->instance = 'instance was created';
return $this;
}
function execute(){
$this->instance .= "and something happened";
return $this;
}
}
然后你可以把它们锁起来
顺便说一下,这可能只是示例代码,但您的实例函数实际上并没有创建实例;) $class->instance()->execute()
应该可以工作,但需要在方法中返回值。$class->instance()->execute()
应该可以工作,但您需要在方法中返回值。链接的一般方法是将$this
作为需要链接的任何方法的返回。因此,对于您的代码,它可能是这样的
class one{
public $instance;
function instance(){
$this->instance = 'instance was created';
return $this;
}
function execute(){
$this->instance .= "and something happened";
return $this;
}
}
所以你会:
$one = new one;
$one->instance()->execute(); // would set one::instance to 'instance was createdand something happened'
$one->instance()->instance()->instance(); // would set one::instance to 'instance was created';
$one->instance()->execute()->execute(); / would set one::instance to 'instance was createdand something happenedand something happened'
链接的一般方法是返回$this
,作为需要链接的任何方法的返回。因此,对于您的代码,它可能是这样的
class one{
public $instance;
function instance(){
$this->instance = 'instance was created';
return $this;
}
function execute(){
$this->instance .= "and something happened";
return $this;
}
}
所以你会:
$one = new one;
$one->instance()->execute(); // would set one::instance to 'instance was createdand something happened'
$one->instance()->instance()->instance(); // would set one::instance to 'instance was created';
$one->instance()->execute()->execute(); / would set one::instance to 'instance was createdand something happenedand something happened'