“return $this”是什么意思?意思是?

发布于 2024-11-06 11:16:15 字数 504 浏览 2 评论 0原文

我试图理解这段代码,但当我到达最后一行时,我没有明白。 :(

我可以请你帮忙看看 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;
}

更新:
为了更好地澄清,我删除了我的评论。

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 ?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

高跟鞋的旋律 2024-11-13 11:16:15

这种编码方式称为流畅界面return $this 返回当前对象,因此您可以编写如下代码:

$object
  ->function1()
  ->function2()
  ->function3()
  ;

而不是:

$object->function1();
$object->function2();
$object->function3();

This way of coding is called fluent interface. return $this returns the current object, so you can write code like this:

$object
  ->function1()
  ->function2()
  ->function3()
  ;

instead of:

$object->function1();
$object->function2();
$object->function3();
-黛色若梦 2024-11-13 11:16:15

这将返回调用该方法的实例。这通常是为了实现流畅的接口,这样你就可以调用类似的东西:

CoolClass::factory('hello')->setOptions(array('coolness' => 5))->sayHello();

其中setOptions< /code> 和 sayHello 将在同一个对象上调用。

This will return the instance this method is called on. This usually done for achieving fluent interfaces so 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.

凶凌 2024-11-13 11:16:15

$this 表示当前对象,即当前正在运行该方法的对象。通过返回 $this 对正在工作的对象的引用,该方法将被发送回调用函数。

所以任何人现在做

 $foo2 = $foo->SetOptions($bar);

$foo2 也指 $foo 。

$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.

So anyone doing

 $foo2 = $foo->SetOptions($bar);

$foo2 now refers to $foo also.

浮世清欢 2024-11-13 11:16:15

你只需创建一个函数链

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

}

,这样你就可以使用它

            My_class obj = new My_class();

            $return = obj->method1($param)->method2($param)->method3($param);

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);
怪我太投入 2024-11-13 11:16:15

$this 将是包含该函数的类。

因此,如果您像这样调用它:

$obj->setOptions($options)

它将返回 $obj,它已使用新选项设置。通常,当这样设置时,您不必捕获返回,因为它会影响对象本身,但它会影响对象本身,因此您可以内联使用它。

$this would be the class that contains that function.

So if you were to call it like:

$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.

甜`诱少女 2024-11-13 11:16:15

如果 SetOptions 方法是 ProgramOptions 类或其他类的一部分,则 $this 将引用包含该方法的类,因此您将传回 ProgramOptions 的实例。

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.

素罗衫 2024-11-13 11:16:15

它是一种常见的 OOP 技术,称为Fluent Interface。它的主要目的是帮助在不支持方法级联的语言(如 PHP)中链接多个方法调用。所以

返回$this;

将返回该类的更新实例(对象),以便它可以在其范围内进行另一个调用。请参阅 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);

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

return $this;

will return an updated instance(object) of that class so it can make another call in its scope. See example in 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文