Benubird
Benubird

Reputation: 19517

How to unbind laravel class?

I bind a class like so:

App::bind('Some_Class', 'Other_Class')

So that calls to App::make('Some_Class') will return an instance of 'Other_Class'. However, later on in the script I want to revert this, so that calls to make will now return the original class.

So far, I've tried these:

App::bind('Some_Class', 'Some_Class')
App::bind('Some_Class', NULL)

Neither has worked - they seem to be causing app to store an instance of the class, which is no good, as I need to able to accept arguments. If the constructor is called with no arguments, it triggers a fatal error. So, how do I undo the binding?

I've even tried using reflection:

App::bind('Some_Class', function() {
    $args = func_get_args();
    $app = array_shift($args);
    $reflection = new ReflectionClass( 'Some_Class' );
    return $reflection->newInstanceArgs($args);
});

And it still doesn't work!

Upvotes: 4

Views: 2531

Answers (2)

Andreas
Andreas

Reputation: 8029

App::offsetUnset('Some_Class') should do it.

If you have an instance of the Application ($app) you can even do unset($app['Some_Class'])

Upvotes: 7

tliokos
tliokos

Reputation: 3636

I believe that

App::instance('Some_Class', new Some_Class);

will do the reversion

Upvotes: 0

Related Questions