php8.5
Home/ Manual/ reference / parallel/ The parallel\Future class

The parallel\Future class

A Future represents the return value or uncaught exception from a task, and exposes an API for cancellation.

Futures

A Future represents the return value or uncaught exception from a task, and exposes an API for cancellation.

Example showing Future as return value

php
<?php
$runtime = new \parallel\Runtime;
$future  = $runtime->run(function(){
    return "World";
});
printf("Hello %s\n", $future->value());
?>

The above example will output something similar to:

output
Hello World

The behaviour of a future also allows it to be used as a simple synchronization point even where the task does not return a value explicitly.

Example showing Future as synchronization point

php
<?php
$runtime = new \parallel\Runtime;
$future  = $runtime->run(function(){
    echo "in child ";
    for ($i = 0; $i < 500; $i++) {
        if ($i % 10 == 0) {
            echo ".";
        }
    }
    echo " leaving child";
});

$future->value();
echo "\nparent continues\n";
?>

The above example will output something similar to:

output
in child .................................................. leaving child
parent continues

Class synopsis

class parallel\Future { }

Future

Source: reference/parallel/parallel.future.xml · from the official PHP manual (php/doc-en)