oop - PHP Have Object Return Different Object -
in few different places call:
$model=new makecall($form);
i have updated makecall
class several changes want take affect after given date. renamed original class makecallold
how can leave calls to:
$model=new makecall($form);
intact , within makecall
this:
class makecall { ... public function __construct($form) { //use legacy makecallold stuff before 2016-10-01 if ($form['date']<'2016-10-01') { $object=new makecallold($form); return $object; } $this->perform several functions , set variables...
this returns empty object of class makecallold
not appear run constructor in makecallold
properties empty. entire object of makecallold
dropped $model
.
what need static factory constructor. way should doing add initialization logic or switch constructors depending on argument.
class makecall { public function __construct($form) { $this->form = $form; } public function showform(){ echo $this->form; } public static function create($form){ //put logic picking object or whatever here! $object = new makecall($form); //do more initializing if want here! return $object; } } $form = "asdasd"; $model= makecall::create($form); $model->showform();
Comments
Post a Comment