1. Go to this page and download the library: Download dima-bzz/fork library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dima-bzz / fork example snippets
useSpatie\Fork\Fork;
$results = Fork::new()
->run(
fn () => (new Api)->fetchData(userId: 1),
fn () => (new Api)->fetchData(userId: 2),
fn () => (new Api)->fetchData(userId: 3),
);
$results[0]; // fetched data of user 1
$results[1]; // fetched data of user 2
$results[2]; // fetched data of user 3
useSpatie\Fork\Fork;
$results = Fork::new()
->run(
function(){
sleep(1);
return'result from task 1';
},
function(){
sleep(1);
return'result from task 2';
},
function(){
sleep(1);
return'result from task 3';
},
);
// this code will be reached this point after 1 second
$results[0]; // contains 'result from task 1'
$results[1]; // contains 'result from task 2'
$results[2]; // contains 'result from task 3'