The child() function executes a child workflow within a parent workflow. Child workflows run independently and can be used to organize complex workflows into smaller, reusable components.
Signature
function child($workflow, ...$arguments): PromiseInterface
Parameters
The child workflow class or name to execute
Optional arguments to pass to the child workflow
Returns
PromiseInterface
React\Promise\PromiseInterface
A promise that resolves to the child workflow’s result
Usage
use Workflow\Workflow;
use function Workflow\{child, activity};
class ParentWorkflow extends Workflow
{
public function execute()
{
// Execute a child workflow
$childResult = yield child(ChildWorkflow::class);
// Execute an activity
$activityResult = yield activity(TestActivity::class);
return 'workflow_' . $activityResult . '_' . $childResult;
}
}
Example with Arguments
use Workflow\Workflow;
use function Workflow\child;
class OrderProcessingWorkflow extends Workflow
{
public function execute($orderId)
{
// Execute child workflows with arguments
$paymentResult = yield child(PaymentWorkflow::class, $orderId);
$inventoryResult = yield child(InventoryWorkflow::class, $orderId);
$shippingResult = yield child(ShippingWorkflow::class, $orderId);
return [
'payment' => $paymentResult,
'inventory' => $inventoryResult,
'shipping' => $shippingResult,
];
}
}
Example with Multiple Children
use Workflow\Workflow;
use function Workflow\{child, all};
class ParallelWorkflow extends Workflow
{
public function execute()
{
// Execute multiple child workflows in parallel
$results = yield all([
child(FirstChildWorkflow::class),
child(SecondChildWorkflow::class),
child(ThirdChildWorkflow::class),
]);
return $results;
}
}