メソッドチェーン2

<?php

class Sum {
	private $sum = 0;
	public static function _(){
		return new self;
	}

	public function add($a){
		$this->sum += $a;
		return $this;
	}
	public function out(&$result){
		$result = $this->sum;
		return $this;
	}
}

Sum::_()->add(1)
		->add(3)
		->out($result1)
		->add(5)
		->add(8)
		->out($result2);
echo "$result1\t$result2";
?>

サンプル2
オークションで例えてみましょう。

<?php

class Auction {
	private $sum=50000;
	public static function _(){
		return new self;
	}

	public function sell($sales){
		$this->sum += $sales;
		return $this;
	}

	public function buy($stock){
		$this->sum -= $stock;
		return $this;
	}

	public function amount(&$result){
		$result = $this->sum;
		return $this;
	}
}

Auction::_()->sell(5000)
			->buy(3000)
			->sell(2000)
			->amount($result1)
			->sell(5000)
			->sell(2000)
			->sell(1000)
			->amount($result2);

echo "$result1\t$result2";
?>