开发者

Detecting end of method chain in PHP?

开发者 https://www.devze.com 2022-12-25 14:28 出处:网络
I cannot find a simple example abo开发者_如何学Pythonut my question above: how can i detect the end of a method chain?

I cannot find a simple example abo开发者_如何学Pythonut my question above: how can i detect the end of a method chain?

I'm just looked Zend_Db_Select for example but this one is too complex for this simple question i think.

Is it possible to catch the 'end' of a method chain in PHP?

thanks, fabrik


No. This is not possible.

Zend_Db_Select does not detect the end of the method chain. Every method you chain just returns $this and when you stop chaining, you stopped chaining. There is no magic something that says: Hey, this is the end of the chain.

Example:

$foo = $this->is()->a()->method()->chain();

If all methods return $this, then $foo will contain the result of chain(), so $this again. You can keep on chaining from there. Of course, the methods will do other things besides returning $this, e.g. set internal state - but that's really all there is about Method chaining.

If your code needs to know that it has reached the end of a chain, you would have to setup the chain before running it, e.g. Chain of Responsibility pattern.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号