PHP code example of tbachert / spi

1. Go to this page and download the library: Download tbachert/spi library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

tbachert / spi example snippets


ServiceLoader::register(Example\Service::class, Example\Implementation::class);

foreach (ServiceLoader::load('Namespace\Service') as $provider) {
    // ...
}

$loader = ServiceLoader::load('Namespace\Service');
for ($it = $loader->getIterator(); $it->valid(); $it->next()) {
    try {
        $provider = $it->current();
    } catch (ServiceConfigurationError) {}
}