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

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -