Parallel in PHP for Dummies? Cooking a Family Dinner

Would you like to understand parallel in PHP apps?
Do you have just 60 seconds?

Imagine you're cooking dinner for your family, and you miss 4 ingredients to make the meal tasty.

Onion, garlic, pepper, and chilli:

Thank God you have exactly 2 kids that want to help you to cook your favorite meal!

You could send them both to get 4 ingredients, but it would be faster if each of them could get just 2 ingredients.

You're rushing them to get it, and after they leave, you realize they forgot their phones. They can't talk to you, and they can't talk to each other. We don't know when they'll be back, if they have enough money or if they found what you need.

We have to wait till everyone gets back to see the result.


How does this Story Look in PHP code?

// 1. what we have and what we want?
$neededIngredients = ['onion', 'garlic', 'pepper', 'chilli'];
$familyMembers = ['son', 'daughter'];

// 2. tell the instructions
$familyMembersCount = count($familyMembers);
$ingredientsChunks = array_chunk($neededIngredients, $familyMembersCount)

// 3. let them free and wait for the result
$foundIngredients = [...];
foreach ($familyMembers as $key => $familyMember) {
    $ingredientsChunk = $ingredientsChunks[$key];
    $foundIngredients[] = $familyMember->findIngredients($ingredientsChunk);
}

return $foundIngredients;

And that's precisely how parallel works ECS!

From Food to Technical Terminology

That's it! Now you understand parallel processes.


Happy coding!




Do you learn from my contents or use open-source packages like Rector every day?
Consider supporting it on GitHub Sponsors. I'd really appreciate it!