6

I cannot find a simple example about 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

fabrik
  • 14,094
  • 8
  • 55
  • 71

1 Answers1

2

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.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • Probably i need to believe you but how can you explain that Zend_Db_Select just doing it? – fabrik Apr 06 '10 at 09:48
  • Looks like i'm just overlooked it again. So this one (Zend_Db_Select) was the worst example because it doesn't fetch anything it just only builds the query. Am i right? – fabrik Apr 06 '10 at 09:55
  • Where do you see Zend_Db_Select doing this? As not every Zend_Db_Select method returns $this and the Zend_Db_Select implements __toString() I think you misunderstand what is going on. – Matijs Apr 06 '10 at 09:55
  • @fabrik Well, yes. More or less. It collects everything you need for creating a SQL query. But there is nothing magical about it. – Gordon Apr 06 '10 at 09:58
  • Thanks for sorting this out and sorry for the unnecessary question. – fabrik Apr 06 '10 at 10:01