<?php

$func = function () {
    $result = (yield adder(1, 2));
    $result = (yield adder($result, 3));
    $result = (yield adder($result, 4));
    echo $result;
};
coroutine($func)->wait();
<?php

adder(1, 2, function ($result) {
    adder($result, 3, function ($result) {
        adder($result, 4, function ($result) {
            echo $result, "\n";
        }, function ($error) {
        });
    }, function ($error) {
    });
}, function ($error) {
});
<?php

adder(1, 2)->then(function ($result) {
    throw Exception('Uh oh');
})->then(function ($result) {
    // Prints result of async operation.
    echo $result, "\n";
})->otherwise(function ($exception) {
    // Handle exception
});
Пример #4
0
 public function test_2()
 {
     $a = 14 + 4;
     $b = __;
     $this->assertEquals(adder($a, $b), 22);
 }
<?php

function adder($a, $b, $success, $error)
{
    // Magic here
}
adder(1, 2, function ($result) {
    echo $result, "\n";
}, function ($error) {
    echo $error, "\n";
});
<?php

adder(1, 2)->then(function ($result) {
    return adder($result, 3);
})->then(function ($result) {
    return adder($result, 4);
})->then(function ($result) {
    echo $result, "\n";
})->otherwise(function ($error) {
    echo $error, "\n";
});
function adder($var1, $var2)
{
    $sum = $var1 + $var2;
    return $sum;
}
?>
 
<html>
<head>
<title>Function with PHP an example</title>
</head>
<body>
<?php 
$a = 10;
$b = 15;
$added = adder($a, $b);
?>

The sum of <?php 
echo $a;
?>
 and <?php 
echo $b;
?>
  is <?php 
echo $added;
?>
.
</body>
</html>