PHP 8.4.22 Released!

Fiber::throw

(PHP 8 >= 8.1.0)

Fiber::throwファイバーの実行を、例外をスローすることで再開する

説明

public function Fiber::throw(Throwable $exception): mixed

現在の Fiber::suspend() への呼び出しから、 指定した例外をスローさせることでファイバーを再開させます。

このメソッドがコールされた時点でファイバーが停止していない場合、 FiberError がスローされます。

パラメータ

exception

現在の Fiber::suspend() への呼び出しから、 ファイバーにスローする例外を指定します。

戻り値

Fiber::suspend() が次回コールされた際に指定した値を返します。 ファイバーから制御が戻った場合は null を返します。 停止する前にファイバーが例外をスローする場合、 このメソッドの呼び出しからスローされます。

<?php

$fiber
= new Fiber(function () {
try {
// 中断ポイントを宣言してファイバーの実行を停止する
Fiber::suspend();
} catch (
Throwable $e) {
echo
$e->getMessage();
}
});

$fiber->start();

// 中断ポイントでスローする例外を渡して
// ファイバーの実行を再開する
$fiber->throw(new Exception('Message of an exception thrown at the current interrupt point'));

?>

上の例の出力は、 たとえば以下のようになります。

Message of an exception thrown at the current interrupt point
add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top