“返回$ this"是什么意思?意思是? [英] What does "return $this" mean?

查看:127
本文介绍了“返回$ this"是什么意思?意思是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图理解这段代码,当我到达最后一行时,我没有理解它. :(

I'm trying to understand this code, and when I arrived at the final line, I didn't get it. :(

我可以帮您找出return $this是什么意思吗?

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.

推荐答案

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

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()
  ;

代替:

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

这篇关于“返回$ this"是什么意思?意思是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆