mercredi 10 juin 2020

How to create a dynamic function name?

I am using carbon which add date depending on frequency. Currently this is my working code.

if ($repetition_frequency == 'Day') {

   $test_date->addDays($repetition);

} else if ($repetition_frequency == 'Week') {

   $test_date->addWeeks($repetition);

}else if ($repetition_frequency == 'Month') {

   $test_date->addMonths($repetition);

}else if ($repetition_frequency == 'Year') {

   $test_date->addYears($repetition);

}

My goal is to remove the if condition and depend what function i will call by $repetition_frequency

Goal:

ex: $repetition_frequency = 'Year';

$test_date->'add'.$repetition_frequency->($repetition);

// i want => $test_date->addYear->($repetition);

I know this won't work. Is there any way to achieve that?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2XR4myO
via IFTTT

Aucun commentaire:

Enregistrer un commentaire