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

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

WBOY
WBOYOriginal
2016-06-23 13:50:331056browse

比如这句:

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

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

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

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

Statement:
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