class ArrayBuilder { public function __set($name, $value){ return $this->$name = $value; } public function __call($name, $value){ if(count($value) == 1){ $this->$name = $value[0]; }else{ $this->$name = $value; } return $this; } public function toArray(){ return get_object_vars($this); } public static function FACTORY(){ return new ArrayBuilder(); } }
Usage:
$x = ArrayBuilder::FACTORY()->hello("World")->digits(1,2,3,4,5)->foo("BaR?")->toArray(); var_dump($x); array(3) { ["hello"]=> string(5) "World" ["digits"]=> array(5) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) } ["foo"]=> string(4) "BaR?" }
Works great for factory scenarios and confusing the $*&! out of the unwary.
Sorry, the comment form is closed at this time.