php - Static classes vs class member access on instantiation -


in versions of php prior 5.4 used static classes instantiate object , call required function, example:

$result = foo::init()->bar(); 

in above example, static function init() instantiates class in contained , returns object. provides method chaining functionality , allows me call bar(), in 1 line of code. static function init() looks this:

static public function init() {    $object = new self();    return $object; } 

now php 5.4 has added support class member access on instantiation, , instead of using static class can following:

$result = (new foo)->bar(); 


my question: old way of using static classes bad, , if so, why? php supports class member access on instantiation, more correct way of accessing class members after object instantiation?

if that's ->init() does, can away (new foo)->bar();, when go dependancy injection route, want create kind of factory 'inject depedencies' on instantiation. factory may full fledged instantiated object, or static method starters, fact of matter is: if need outside access injected class (database handlers, settings) or possibly in future, , don't want abuse globals or singletons (which kind of globals...), you'll thankfull have 1 method / class objects instantiated rather sprinkled through codebase.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -