<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/langref.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'language.fibers.php',
    1 => 'Fibers',
    2 => 'Fibers',
  ),
  'up' => 
  array (
    0 => 'langref.php',
    1 => 'Language Reference',
  ),
  'prev' => 
  array (
    0 => 'language.exceptions.extending.php',
    1 => 'Extending Exceptions',
  ),
  'next' => 
  array (
    0 => 'language.generators.php',
    1 => 'Generators',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/fibers.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="language.fibers" class="chapter">
 <h1 class="title">Fibers</h1>


 <div class="simplesect" id="language.fibers.overview">
  <h3 class="title">Fibers overview</h3>
  <p class="verinfo">(PHP 8 &gt;= 8.1.0)</p>
  <p class="para">
   Fibers represent full-stack, interruptible functions. Fibers may be suspended from anywhere in the call-stack,
   pausing execution within the fiber until the fiber is resumed at a later time.
  </p>
  <p class="para">
   Fibers pause the entire execution stack, so the direct caller of the function does not need to change how it
   invokes the function.
  </p>
  <p class="para">
   Execution may be interrupted anywhere in the call stack using <span class="methodname"><a href="fiber.suspend.php" class="methodname">Fiber::suspend()</a></span>
   (that is, the call to <span class="methodname"><a href="fiber.suspend.php" class="methodname">Fiber::suspend()</a></span> may be in a deeply nested function or not
   even exist at all).
  </p>
  <p class="para">
   Unlike stack-less <span class="classname"><a href="class.generator.php" class="classname">Generator</a></span>s, each <span class="classname"><a href="class.fiber.php" class="classname">Fiber</a></span> has its own call stack,
   allowing them to be paused within deeply nested function calls. A function declaring an interruption point
   (that is, calling <span class="methodname"><a href="fiber.suspend.php" class="methodname">Fiber::suspend()</a></span>) need not change its return type, unlike a function using
   <a href="language.generators.syntax.php#control-structures.yield" class="link"><code class="literal">yield</code></a> which must return a <span class="classname"><a href="class.generator.php" class="classname">Generator</a></span> instance.
  </p>
  <p class="para">
   Fibers can be suspended in any function call, including those called from within the PHP VM, such as functions
   provided to <span class="function"><a href="function.array-map.php" class="function">array_map()</a></span> or methods called by <a href="control-structures.foreach.php" class="link"><code class="literal">foreach</code></a> on an
   <span class="classname"><a href="class.iterator.php" class="classname">Iterator</a></span> object.
  </p>
  <p class="para">
   Once suspended, execution of the fiber may be resumed with any value using <span class="methodname"><a href="fiber.resume.php" class="methodname">Fiber::resume()</a></span>
   or by throwing an exception into the fiber using <span class="methodname"><a href="fiber.throw.php" class="methodname">Fiber::throw()</a></span>. The value is returned
   (or exception thrown) from <span class="methodname"><a href="fiber.suspend.php" class="methodname">Fiber::suspend()</a></span>.
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Prior to PHP 8.4.0, switching fibers during the execution of an object
    <a href="language.oop5.decon.php#language.oop5.decon.destructor" class="link">destructor</a> was not
    allowed.
   </span>
  </p></blockquote> 

  <div class="example" id="language.fiber.example.basic">
   <p><strong>Example #1 Basic usage</strong></p>
   <div class="example-contents">
    <div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$fiber </span><span style="color: #007700">= new </span><span style="color: #0000BB">Fiber</span><span style="color: #007700">(function (): </span><span style="color: #0000BB">void </span><span style="color: #007700">{<br />   </span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">Fiber</span><span style="color: #007700">::</span><span style="color: #0000BB">suspend</span><span style="color: #007700">(</span><span style="color: #DD0000">'fiber'</span><span style="color: #007700">);<br />   echo </span><span style="color: #DD0000">"Value used to resume fiber: "</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">$value </span><span style="color: #007700">= </span><span style="color: #0000BB">$fiber</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">start</span><span style="color: #007700">();<br /><br />echo </span><span style="color: #DD0000">"Value from fiber suspending: "</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$fiber</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">resume</span><span style="color: #007700">(</span><span style="color: #DD0000">'test'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

   <div class="example-contents"><p>The above example will output:</p></div>
   <div class="example-contents screen">
    <div class="cdata"><pre>
Value from fiber suspending: fiber
Value used to resume fiber: test
</pre></div>
   </div>
  </div>

 </div>

</div>
<?php manual_footer($setup); ?>