1

I have method that creates sms objects from user numbers in a foreach loop

$phones = "3444425,455667"; //get phone numbers from textarea
$phones = chop($phones,","); //remove last comma
$phones = multiexplode(array(","," ","\r\n",".","|",":"),$phones); //reformat and convert to array
$message = "foobar foo"; // get the message from form field

//**loop through phone numbers from form field and reformat */

foreach($phones as $phone){

    //create multiple Sms Object(s)

    $sms = new Sms($sender, $phone, $message, "18");


} //close loop

How can I pass the sms objects from the loop to my function like this

$response = $instance->sendBatchSMS($sms1,$sms2,$sms3,etc);

and not

$response = $instance->sendBatchSMS($sms1); 
$response = $instance->sendBatchSMS($sms2); 
$response = $instance->sendBatchSMS($sms3);
Ihor Vyspiansky
  • 918
  • 1
  • 6
  • 11
Edward Muss
  • 49
  • 1
  • 10
  • 1
    Store sms objects in array and pass this array. – u_mulder Aug 06 '20 at 06:58
  • 1
    or using splat operator https://stackoverflow.com/questions/41124015/meaning-of-three-dot-in-php – catcon Aug 06 '20 at 06:59
  • Is it user inputted phone numbers you are sending too? If yes then I'll advice you to not do the multiexplode. Better to use just an explode and inform the user on what the delimiter is. The way you do it may seem helpful to the user but then someone comes along and uses `;` – Andreas Aug 06 '20 at 08:44

1 Answers1

1

try this:

$list = [];
foreach($phones as $phone){
    $list[] = new Sms($sender, $phone, $message, "18");
}
call_user_func_array(array($instance, "sendBatchSMS"), $list);
Edson Magombe
  • 313
  • 2
  • 14