PHP code example of hyperf-helper / dependency

1. Go to this page and download the library: Download hyperf-helper/dependency 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/ */

    

hyperf-helper / dependency example snippets



# config/container.php


/**
 * Initialize a dependency injection container that implemented PSR-11 and return the container.
 */

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  [email protected]
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

use Hyperf\Di\Container;
use Hyperf\Di\Definition\DefinitionSourceFactory;
use Hyperf\Utils\ApplicationContext;

$container = new Container((new DefinitionSourceFactory(true))());

if (! $container instanceof \Psr\Container\ContainerInterface) {
    throw new RuntimeException('The dependency injection container is invalid.');
}

/*********     start      ********/
// Add this line between `new Container` and `setContainer()`
\HyperfHelper\Dependency\Annotation\Collector\DependencyCollector::walk([$container, 'define']);
/*********      end       ********/

return ApplicationContext::setContainer($container);



declare(strict_types=1);

namespace App\Service;

use HyperfHelper\Dependency\Annotation\Dependency;

// add Dependency annotation
#[Dependency()]
class ExampleService implements ExampleServiceInterface {
    // anything
}




declare(strict_types=1);

namespace App\Controller;

use Hyperf\Di\Annotation\Inject;
use App\Service\ExampleServiceInterface;


class FooController {

    #[Inject]
    protected ExampleServiceInterface $service;
}