Home > Backend Development > PHP Tutorial > 这句话如何理解呢

这句话如何理解呢

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:45:17
Original
887 people have browsed it

Model('seo')->type('index')->show();
Copy after login

最后翻译一下


回复讨论(解决方案)

Model('seo') 得到一个 seo 对象
->type('index') 执行 seo 对象的 type 方法
->show(); 执行 seo 对象的 show 方法

Model('seo') 得到一个 seo 对象
->type('index') 执行 seo 对象的 type 方法
->show(); 执行 seo 对象的 show 方法



我的疑惑是,为啥用两个箭头呢,那样符合语法规范吗,因为我的理解 Model('seo')->type('index')
它是对象实例化后调用的一个方法,而它后面又再调用一个方法,但是 Model('seo')->type('index')
它此时可不是对象啊,怎么能行呢

如果能连续用箭头的话,那么一个类中有很多方法,是不是说,能用一长串箭头调用的写法呢


Model('seo') 得到一个 seo 对象
->type('index') 执行 seo 对象的 type 方法
->show(); 执行 seo 对象的 show 方法



我的疑惑是,为啥用两个箭头呢,那样符合语法规范吗,因为我的理解 Model('seo')->type('index')
它是对象实例化后调用的一个方法,而它后面又再调用一个方法,但是 Model('seo')->type('index')
它此时可不是对象啊,怎么能行呢



调用的方法 返回 $this 即可这样级联

class  Orange{    public function a(){        echo "a";        return $this;    }    public function b(){        echo "b";        return $this;    }    public function c(){        echo "c";        return $this;    }}$orange = new Orange();$orange->a()->c()->b();
Copy after login


acb
Copy after login

只要这些函数(方法)返回的是对象,就可以用这种连续操作的写法

但是Model('seo')->type('index') 它此时可不是对象啊
你怎么知道他不是对象?

突然明白了!原来javascript中的连缀就是用的这个原理。

突然明白了!原来javascript中的连缀就是用的这个原理。


只不过javascript中用的是 点而不是 箭头罢 了
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template