在一行语句里调用一个类的多个方法,该怎么写?

WBOY
Release: 2016-06-23 13:50:33
Original
1072 people have browsed it

比如这句:

$this->m_button6PaneInfo->Movable( false )->Dock()->Fixed()->BottomDockable( false )->TopDockable( false )->Layer( 10 )->ToolbarPane();
Copy after login

$this->m_button6PaneInfo 是一个类实例。后面的全是该类的方法。
这条语句是无法通过的,至少我的php版本无法通过。

除了拆成多行外,这条该怎么改?才能正确执行?
或者升级到哪个版本?才可以这样写


回复讨论(解决方案)

只要那些方法返回的是 $this 那么就不会不能通过

只要那些方法返回的是 $this 那么就不会不能通过


用清晰的代码表述一下:
<?phpclass testcls{	function fun1(){		echo 'fun1';	}	function fun2(){		echo 'fun1';	}	function fun3(){		echo 'fun3';	}}$test = new testcls();$test->fun1()->fun2()->fun3();?>
Copy after login

$test->fun1()->fun2()->fun3(); 这样写不行。
那该怎么写?用一行语句调用3个方法。

这样写

class testcls{    function fun1(){        echo 'fun1';        return $this;    }    function fun2(){        echo 'fun1';        return $this;    }    function fun3(){        echo 'fun3';        return $this;    }}$test = new testcls();$test->fun1()->fun2()->fun3();
Copy after login

链式操作的前提,每次操作都返回对象本身。

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!