PHP code example of michael-rubel / laravel-enhanced-container

1. Go to this page and download the library: Download michael-rubel/laravel-enhanced-container 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/ */

    

michael-rubel / laravel-enhanced-container example snippets


call(ServiceInterface::class, context: static::class);

// The `call` method automatically resolves the implementation from the interface you passed.
// If you pass the context, the proxy tries to resolve contextual binding instead of global binding first.

class Service
{
    public function yourMethod(int $count): int
    {
        return $count;
    }
}

$this->app->bind(ServiceInterface::class, Service::class);

call(ServiceInterface::class)->yourMethod(100);

$this->app->bind(ApiGatewayContract::class, InternalApiGateway::class);

bind(ApiGatewayContract::class)->method('performRequest', function ($service, $app, $params) {
    // Note: you can access `$params` passed to the method call.

    return true;
});

$apiGateway = call(ApiGatewayContract::class);
$request = $apiGateway->performRequest();
$this->assertTrue($request);

$this->app->register(LecServiceProvider::class);

use MichaelRubel\EnhancedContainer\Core\Forwarding;

Forwarding::enable()
    ->from(Service::class)
    ->to(Repository::class);

Forwarding::enable()
    ->from(Service::class)
    ->to(Repository::class)
    ->from(Repository::class)
    ->to(Model::class);