Possible Duplicate:
PHP method chaining?
In a lot of APIs I’ve worked with, I’ve seen this sort of thing:
$object->method()->anotherMethod();
From the tutorials on OOP that I’ve read, this is how classes are written,
<?php
class myClass {
public method() {
// do something
}
}
?>
When should this be used, and how can it be done? Apologies, but I am new to OOP.
If your method returns $this, you will be able to use the above style (
$object->method()->anotherMethod()). This can be done only in cases where your method is not expected to return something else, e.g. a method named likegetSomething()is expected to returnSomething, but if you have a method that has no relevant value to return, you can just return $this, allowing method call chains.