I love the way CodeIgniter explains method chaining by saying.
Method chaining allows you to simplify your syntax by connecting multiple functions.
As explained above it simply lets you do things like
FileObject->OpenFile('FileName')->AppendLine('Hello World!')->CloseFile()
Its neat. But in PHP, you have to have PHP5.x. This wonderful thing is also available in JavaScript. From jQuery home page
$("p.neat").addClass("ohmy").show("slow");
How to do method chaining?
To have an object chain methods every method in that object must return a reference to itself. Easy. Example?
<?php
class MethodChainingExample{
public function methodOne(){
echo __METHOD__." \n";
return $this;
}
public function anotherMethod(){
echo __METHOD__." \n";
return $this;
}
public function oneMoreMethod(){
echo __METHOD__." \n";
return $this;
}
}
$example = new MethodChainingExample;
$example->methodOne()->anotherMethod()->oneMoreMethod();

















