PHP Static Instantiation – The Underscore Method
Lets make this underscore method a standard for static instantiation.
_() reads as “instance”
class Foo { public static function _() { return new self(); } public $n = 100; function bar() { $this->n--; return $this; } function __toString() { return $this->n." bottles of beer on the wall"; } } echo Foo::_()->bar()->bar()->bar()->bar()->bar(); /* Reads echo Foo ^instance bar, bar, bar, bar, bar. Output 95 bottles of beer on the wall */
Comments(0)