Scope::provideScope

(PHP 8.6+, True Async 1.0)

public function provideScope(): Scope

Implémentation de l’interface ScopeProvider. Retourne l’objet scope lui-même. Cela permet d’utiliser Scope partout où un ScopeProvider est attendu.

Valeur de retour

Scope — l’objet scope courant.

Exemples

Exemple #1 Utilisation en tant que 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 implémente lui-même ScopeProvider
runInScope($scope);

$scope->awaitCompletion();

Exemple #2 Polymorphisme avec 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";
        });
    }
}

// Fonctionne avec Scope et ServiceContainer
$scope = new Scope();
startWorkers($scope, 3);

$container = new ServiceContainer();
startWorkers($container, 3);

Voir aussi