construct - PHP: using $this in constructor -
i have idea of using syntax in php. illustrates there different fallback ways create object
function __construct() { if(some_case()) $this = method1(); else $this = method2(); }
is nightmare? or works?
or works?
it doesn't work. can't unset or fundamentally alter object being created in constructor. can not set return value. can set object's properties.
one way around having separate "factory" class or function, checks condition , returns new instance of correct object so:
function factory() { if(some_case()) return new class1(); else return new class2(); }
see also:
Comments
Post a Comment