Passing arguments to a native PHP function as an array to start the process in the background using a library like symfony/process

Viewed 58

I have a simple PHP class, I am trying to call the method/function within my class while using Symfony/process library and pass some arguments [in an array and/or boolean formats] to my function!

What is the objective?

I want to call the function "update_mysql_records" while being able to pass my array "$customers_data" as an argument to this function while calling it via Symfony/process to be able to run this call in the background as a PHP background process!

Why do I use Symfony/process library (https://github.com/symfony/process)?

I want to be able to run a PHP background process in both Windows and Linux OS and Symfony/process seems to be the only library that can do that without any issues on Windows OS as well as Linux.

Here is an example of what I have:

/**
* ### TEST CLASS
*/
class TestClass 
{
    public $returned_data = array();

    public static function update_mysql_records($customers_data = array(), $insert_new_customers=true ){
        
        if(!empty($customers_data))
        foreach($customers_data as $customer_id=>$customer_data ):
            // do something with the customer data here 
            // for example update customer data in MYSQL database
            // or insert new data if insert_new_customers was true
            // then return something!
            $returned_data[] = $customer_data;
        endforeach;
        return $returned_data;
    }
}
    // this is the array I wish to be able to pass to my function as an argument while calling my funcction using symfony process
    $customer_data = [
        1 => ['name'=>'xxx1', 'phone'=>'0123456', 'country_iso_code'=>'USA'],
        7 => ['name'=>'XX2', 'phone'=>'01234567', 'country_iso_code'=>'DNK'],
        12 => ['name'=>'XX3', 'phone'=>'012345678', 'country_iso_code'=>'FIN'],
        50 => ['name'=>'XX4', 'phone'=>'01234569', 'country_iso_code'=>'CAN'],
    ];

a few notes:

  • I am not using any frameworks! this is bare-bone PHP we are talking about!
  • I know there are many libraries out there or even simple exec(), etc.. functions available in PHP. but I want to be able to use Symfony/process for this project
  • It would be also very helpful if you could also point out how to make sure the process does not exist if we exit the page using this background process call.
0 Answers
Related