Method chaining only makes sense when you are using setters.
In combination with PHPDoc most IDE's (like NetBeans) will support code completion/autocomplete feature again.
p.s note that method chaining can give your more chance on errors like PHP Fatal error: Call to a member function .... on a non-object in .... when mixing in more classes with returning
class Example {
protected $var = null;
public function __construct() {
}
public function __destruct() {
unset($this->var); // explicit memory clearing
}
/**
* @return \Example
*/
public static function create() {
return new self();
}
/**
* @return \Example
*/
public static function getInstance() {
return self::create();
}
/**
* @return \Example
*/
public static function newInstance() {
return self::create();
}
/**
*
* @param mixed $var
* @return \Example
*/
public function setThing1($var) {
$this->var = $var;
return $this;
}
/**
*
* @param mixed $var
* @return \Example
*/
public function setThing2($var) {
$this->var = $var;
return $this;
}
}
Example::create()->setThing1(1)->setThing2('2');