Scope::provideScope
(PHP 8.6+, True Async 1.0)
public function provideScope(): Scope
Implementation of the ScopeProvider interface. Returns the scope object itself. This allows Scope to be used anywhere a ScopeProvider is expected.
Return Value
Scope — the current scope object.
Examples
Example #1 Using as a ScopeProvider
<?php
use Async\Scope;
use Async\ScopeProvider;
function runInScope(ScopeProvider $provider): void {
$scope = $provider->provideScope();
$scope->spawn(function() {
echo "Running in the provided scope\n";
});
}
$scope = new Scope();
// Scope itself implements ScopeProvider
runInScope($scope);
$scope->awaitCompletion();
Example #2 Polymorphism with ScopeProvider
<?php
use Async\Scope;
use Async\ScopeProvider;
class ServiceContainer implements ScopeProvider {
private Scope $scope;
public function __construct() {
$this->scope = new Scope();
}
public function provideScope(): Scope {
return $this->scope;
}
}
function startWorkers(ScopeProvider $provider, int $count): void {
$scope = $provider->provideScope();
for ($i = 0; $i < $count; $i++) {
$scope->spawn(function() use ($i) {
echo "Worker $i started\n";
});
}
}
// Works with both Scope and ServiceContainer
$scope = new Scope();
startWorkers($scope, 3);
$container = new ServiceContainer();
startWorkers($container, 3);
See Also
- Scope::inherit — Create a child scope
- Scope::spawn — Spawn a coroutine in the scope