Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.4] Bind no longer accepts an array containing aliases. #17793

Closed
jralph opened this issue Feb 6, 2017 · 11 comments
Closed

[5.4] Bind no longer accepts an array containing aliases. #17793

jralph opened this issue Feb 6, 2017 · 11 comments

Comments

@jralph
Copy link

jralph commented Feb 6, 2017

  • Laravel Version: 5.4.x
  • PHP Version: 7.0
  • Database Driver & Version: N\A

Description:

In 5.4 it is no longer possible to use bind by passing in an array with an alias. This change doesn't appear to be documented anywhere, and the given error message when this fails doesn't help identify the problem that easily.

$this->app->bind(['some.name' => MyClass::class], function () {
    // Do something here
});

Steps To Reproduce:

Put this code in a service provider.

$this->app->bind(['some.name' => MyClass::class], function () {
    // Do something here
});
@themsaid
Copy link
Member

themsaid commented Feb 7, 2017

check your syntax and please share the error you receive.

@jralph
Copy link
Author

jralph commented Feb 7, 2017

Sorry, I've updated the code to include the missing bracket. I'll get an exact error message for you shortly.

@jralph
Copy link
Author

jralph commented Feb 7, 2017

Ok, on a blank L5.4 install using this in the app service provider.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind(['some.alias' => \Illuminate\Support\Str::class], function () {
            return new \Illuminate\Support\Str();
        });
    }
}

I get the following error:

[2017-02-07 13:09:06] local.ERROR: ErrorException: Illegal offset type in unset in /some/path/to/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php:988
Stack trace:
#0 /some/path/to/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(988): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Illegal offset ...', '/some/path/to/la...', 988, Array)
#1 /some/path/to/laravel/vendor/laravel/framework/src/Illuminate/Container/Container.php(204): Illuminate\Container\Container->dropStaleInstances(Array)
#2 /some/path/to/laravel/app/Providers/AppServiceProvider.php(26): Illuminate\Container\Container->bind(Array, Object(Closure))
#3 /some/path/to/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(568): App\Providers\AppServiceProvider->register()
#4 /some/path/to/laravel/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php(74): Illuminate\Foundation\Application->register(Object(App\Providers\AppServiceProvider))
#5 /some/path/to/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(543): Illuminate\Foundation\ProviderRepository->load(Array)
#6 /some/path/to/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/RegisterProviders.php(17): Illuminate\Foundation\Application->registerConfiguredProviders()
#7 /some/path/to/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(208): Illuminate\Foundation\Bootstrap\RegisterProviders->bootstrap(Object(Illuminate\Foundation\Application))
#8 /some/path/to/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(270): Illuminate\Foundation\Application->bootstrapWith(Array)
#9 /some/path/to/laravel/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(115): Illuminate\Foundation\Console\Kernel->bootstrap()
#10 /some/path/to/laravel/artisan(35): Illuminate\Foundation\Console\Kernel->handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#11 {main}

@wizbit
Copy link

wizbit commented Feb 7, 2017

The commit that removed the functionality: 1a1969b

@GrahamCampbell I've also never seen anyone use that array functionality before.

@fernandobandeira
Copy link
Contributor

fernandobandeira commented Feb 7, 2017

Removed feature because it's already possible by calling the alias method

$this->app->alias(\Illuminate\Support\Str::class, 'some.alias');

You just have to refactor it...

@jralph
Copy link
Author

jralph commented Feb 7, 2017

@fernandobandeira Thanks, thats what I've done.

It's not great when a feature that can break quite a lot and throws an error message that doesn't really help out is just randomly removed. This really should be something documented in the migration docs from 5.3 to 5.4.

The comment in the commit also mentions that it was not documented, there was a comment that explained the functionality so it was.

jralph referenced this issue Feb 7, 2017
Removing some old methods such as share() in favor of the more common
and documented singleton().
@themsaid
Copy link
Member

themsaid commented Feb 7, 2017

@jralph sorry about that, it should have been documented. Will make sure to include that in the upgrade guide.

@themsaid
Copy link
Member

themsaid commented Feb 7, 2017

Docs updated, thanks :)

@themsaid themsaid closed this as completed Feb 7, 2017
@m4tthumphrey
Copy link

This is crazy. What is the reason for removing this? Surely it can stay, as obviously people are using it.

@jralph
Copy link
Author

jralph commented Mar 8, 2017

I agree. Removing code because "I've also never seen anyone use that..." is how you break things.

Sort of like a car engineer say "I've never seen people use brakes, so I'm removing them." without any warning or telling the new car users that the brakes are missing.

@m4tthumphrey
Copy link

@jralph Unfortunately Laravel does have history with that mantra.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants