In a few different places I call:
$model=NEW MakeCall($form);
I have updated the MakeCall
class with several changes that I want to take affect after a given date. I renamed the original class MakeCallOld
How can I leave my calls to:
$model=NEW MakeCall($form);
intact and within MakeCall
do this:
class MakeCall
{ ...
public function __construct($form)
{
//Use Legacy MakeCallOld for stuff before 2016-10-01
if ($form['date']<'2016-10-01') {
$object=NEW MakeCallOld($form);
return $object;
}
$this->perform several functions and set variables...
This currently just returns an empty object of class MakeCallOld
but it does not appear to run the constructor in MakeCallOld
as all properties are empty. I would just like the entire object of MakeCallOld
dropped into $model
.
What you need is a static factory constructor. This is the way you should be doing it to add initialization logic or switch constructors depending on the argument.
class MakeCall
{
public function __construct($form)
{
$this->form = $form;
}
public function showForm(){
echo $this->form;
}
public static function create($form){
//put logic for picking object or whatever here!
$object = new MakeCall($form);
//Do more initializing if you want here!
return $object;
}
}
$form = "asdasd";
$model= MakeCall::create($form);
$model->showForm();
Constructors don't have a return value, so by saying return $object
you are simply ending the control flow there and doing nothing else, never reaching "perform several functions and set variables". Depending on your structure you should consider making MakeCall
inherit from MakeCallOld
, then you can simply call parent::__construct();
in MakeCall
's constructor.