php “返回 $this”是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5956999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does "return $this" mean?
提问by MEM
I'm trying to understand this code, and when I arrived at the final line, I didn't get it. :(
我试图理解这段代码,当我到达最后一行时,我没有明白。:(
Can I have your help in order to find out, what does return $this
mean ?
我可以请你帮忙看看是什么return $this
意思吗?
public function setOptions(array $options) {
$methods = get_class_methods($this);
foreach ($options as $key => $value) {
$method = 'set' . ucfirst($key);
if (in_array($method, $methods)) {
$this->$method($value);
}
}
//???? - return what ?
return $this;
}
Update:
I've removed my comments for better clarification.
更新:
为了更好的说明,我删除了我的评论。
回答by Maerlyn
This way of coding is called fluent interface. return $this
returns the current object, so you can write code like this:
这种编码方式称为流畅界面。return $this
返回当前对象,因此您可以编写如下代码:
$object
->function1()
->function2()
->function3()
;
instead of:
代替:
$object->function1();
$object->function2();
$object->function3();
回答by Jakub Hampl
This will return the instance this method is called on. This usually done for achieving fluent interfacesso you can call stuff like:
这将返回调用此方法的实例。这通常是为了实现流畅的接口,因此您可以调用以下内容:
CoolClass::factory('hello')->setOptions(array('coolness' => 5))->sayHello();
Where both setOptions
and sayHello
would be called on the same object.
其中setOptions
和sayHello
将在同一个对象上调用。
回答by Doug T.
$this
means the current object, the one the method is currently being run on. By returning $this
a reference to the object the method is working gets sent back to the calling function.
$this
表示当前对象,该方法当前正在运行的对象。通过返回$this
对正在工作的对象的引用,该方法将被发送回调用函数。
So anyone doing
所以任何人都在做
$foo2 = $foo->SetOptions($bar);
$foo2 now refers to $foo also.
$foo2 现在也指 $foo。
回答by John
$this would be the class that contains that function.
$this 将是包含该函数的类。
So if you were to call it like:
所以如果你这样称呼它:
$obj->setOptions($options)
$obj->setOptions($options)
it's going to return $obj, which has been set with the new options. Generally when something is set like this, you don't have to capture the return, because it's affecting the object itself, but it makes it so you can use it inline.
它将返回 $obj,它已使用新选项设置。通常,当某些内容设置为这样时,您不必捕获返回,因为它会影响对象本身,但它使您可以内联使用它。
回答by Lloric Mayuga Garcia
you just can create a function chain
你只需要创建一个函数链
class My_class
{
public function method1($param)
{
/*
* logic here
*/
return $this;
}
public function method2($param)
{
/*
* logic here
*/
return $this;
}
public function method3($param)
{
/*
* logic here
*/
return $this;
}
}
so you can use this
所以你可以使用这个
My_class obj = new My_class();
$return = obj->method1($param)->method2($param)->method3($param);
回答by CodeMinion
If the SetOptions method is part of a ProgramOptions class or something, $this would refer to the class containing the method, so you would be passing back an instance of ProgramOptions.
如果 SetOptions 方法是 ProgramOptions 类或其他东西的一部分,则 $this 将引用包含该方法的类,因此您将传回 ProgramOptions 的实例。
回答by Steve Junior
Its a common OOP technique called Fluent Interface. It main purpose to help chain multiple method calls in languages, that do not support method cascading, like PHP. So
它是一种常见的 OOP 技术,称为Fluent Interface。它的主要目的是帮助用不支持方法级联的语言(如 PHP)链接多个方法调用。所以
return $this;
返回 $this;
will return an updated instance(object) of that class so it can make another call in its scope. See example in PHP,
将返回该类的更新实例(对象),以便它可以在其范围内进行另一个调用。请参阅 PHP 中的示例,
class Class_Name {
private field1;
private field2;
private field3;
public function setField1($value){
$this->field1 = $value;
return $this;
}
public function setField2($value){
$this->field2 = $value;
return $this;
}
public function setField3($value){
$this->field3 = $value;
return $this;
}
}
$object = new Class_Name();
$object->setField1($value1)->setField2($value2)->setField3($value3);