Home > Backend Development > PHP Tutorial > javascript - 如何用php实现类似js的链式操作?

javascript - 如何用php实现类似js的链式操作?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:06:54
Original
1066 people have browsed it

<code>$str = new str();
//类似这种链式操作,如何实现?
$str->str()->length();</code>
Copy after login
Copy after login

回复内容:

<code>$str = new str();
//类似这种链式操作,如何实现?
$str->str()->length();</code>
Copy after login
Copy after login

Class MyClass {

    public function doWork() {
        echo "work done\n";
        return $this;
    }
    
    public function doAnotherWork() {
        echo "another work done\n";
        return $this;
    }

}
(new MyClass())->doWork()->doAnotherWork();
Copy after login

ThinkPHP的数据库操作就支持这种方式,你可以看看他的实现方法。

return $this;
Copy after login

https://segmentfault.com/a/1190000003069...

很简单,返回对象即可。

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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template