3

In Laravel's Container Illuminate\Container\Container, in the bind method, we see this bit of code:

    // If the abstract type was already resolved in this container we'll fire the
    // rebound listener so that any objects which have already gotten resolved
    // can have their copy of the object updated via the listener callbacks.
    if ($this->resolved($abstract)) {
        $this->rebound($abstract);
    }

It appears to allow to rebinding of any previously resolved abstract type. In what use cases would you need to rebind an abstract type? In the rebound method, we see that if indeed the abstract type was previously resolved, we run all the rebound callbacks.

    /**
 * Fire the "rebound" callbacks for the given abstract type.
 *
 * @param  string  $abstract
 * @return void
 */
protected function rebound($abstract)
{
    $instance = $this->make($abstract);

    foreach ($this->getReboundCallbacks($abstract) as $callback) {
        call_user_func($callback, $this, $instance);
    }
}

Can someone give an real-life example as to what type of situation you would rebind an abstract type, and what type of callbacks you would need to call?

1 Answer 1

3

This mechanism allows you to define how any implementations registered in the future should be initialized.

For example: Imagine that you had a RPC client that connects to some server based on configuration files. And you would want any client code to be able to extend your Client class and register their own implementations, but without burdening them with having to initialize the client with the input from the settings file manually. (By "client code" we mean any code that would use your library, the term "client" is confusing here.)

(I wanted to do a similar thing in my Laravel project because I wanted to define the path to my config (the rpc.client) only once and not every time that I register different Client implementation. I did not want the rest of the application to care about such things).

Example implementation:

class CommunicationsServiceProvider extends ServiceProvider
{

/**
 * Register the application services.
 *
 * @return void
 */
public function register()
{
    $this->app->singleton(ClientInterface::class, function(Application $app)
    {
        return null; // Required to trigger the rebinding event with the second bind.
    });

    $this->app->rebinding(ClientInterface::class, function(Application $app, $client)
    {
        $options = $app['config']->get('rpc.client');

        /** @var Client $client */
        $client->configure($options);
        return $client;
    });

    $this->app->singleton(ClientInterface::class, function(Application $app)
    {
        $client = new Client ([]); // We intentionally use empty array to demonstrate 
                                   // that the config will be properly injected anyway.
        return $client;
    });

    // Note that the callback in the binding() method will be called, so even though 
    // we initialize the new Client with empty set of settings (we provide the [] into
    // the constructor), it will be properly initialized by the rebinding() callback.

So every time anyone registers a new Client implementation (via App::bind() etc.) the callback defined in rebinding() will be called on the instance.

Conclusion

What I wonder about thought is whether it is a good idea to use the rebinding() method at all when it is not documented in Laravel docs and is not part of the Container contract (the Illuminate\Contracts\Container\Container interface) and is only a part of the concrete implementation (the Illuminate\Container\Container class ).

It seems that for the same usecase you could use the "proper" (documented and present in public API) resolving() method, which will cause the configure($options) method to be called every time someone resolves this binding (contrary to this case where it is only called once—when the Client is registered).

Sources

I have first noted this functionality in this tutorial, but honestly I did not properly understand it from the example there and had to experiment with it on my own.

Disclaimer

Please note that I am by no stretches of imagination an expert on this topic. But I have currently been looking into the Laravel IoC and this method so I thought that I might provide some insight.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.