3

PHP loads classes into memory and never frees them as far as I am aware. This can be problematic when using attributes.

I am wondering whether fibers could be used. How independent is a fiber? Ultimately the question is this: if a PHP fiber loads a class, is that going to be autoloaded for the rest of the PHP code or is it a separate memory space?

chx
  • 11,270
  • 7
  • 55
  • 129

2 Answers2

2

Code loaded during Fiber execution is available afterwards. Running the following code in Drupal bootstrapped environment:

var_dump(class_exists('\Drupal\Core\Action\Plugin\Action\DeleteAction', FALSE));
$fiber = new Fiber(function() : void {
  var_dump(class_exists('\Drupal\Core\Action\Plugin\Action\DeleteAction', TRUE));
});
$fiber->start();
var_dump(class_exists('\Drupal\Core\Action\Plugin\Action\DeleteAction', FALSE));

produces the following output:

bool(false)
bool(true)
bool(true)
alexpott
  • 36
  • 1
2

Fibers share the same memory space. Each fiber just has its own call stack. If you load a class in a fiber, it's loaded into the process heap, not in the fibers stack allocated memory.

kelunik
  • 6,750
  • 2
  • 41
  • 70