Error:

[caption id=“attachment_248” align=“aligncenter” width=“300”]laravel-ioc-binding-exception laravel-ioc-binding-exception[/caption]

Context that caused this eror

[caption id=“attachment_249” align=“aligncenter” width=“300”]test controller test controller[/caption]

[caption id=“attachment_250” align=“aligncenter” width=“300”]foo service with php primitive type foo service with php primitive type[/caption]

When working with laravel 5, have you ever ask yourself that how** Laravel IOC** work, how **Laravel IOC** can handle concrete class binding instead of defining in somewhere (for laravel in Service providers).

Basically, if you inject a concrete class to another class, it will automatically bind to that class by laravel Container

Overview of Laravel Container (make, build, getDependencies, resolveClass (or nonClass))

Screenshot from 2016-03-14 09:38:57

laravel 5 container class: make function build function getDependencies function

By default, If you inject a concrete class to another class, its will automatically bind and resolve by Laravel container.

[caption id=“attachment_252” align=“aligncenter” width=“300”]Service Container Laravel The PHP Framework For Web Artisans Service Container Laravel The PHP Framework For Web Artisans[/caption]

[caption id=“attachment_247” align=“aligncenter” width=“300”]laravel ioc build class based on concrete class name laravel ioc build class based on concrete class name[/caption]

but, in case in the constructor of some class you inject to a string or non-class parameter, how laravel 5 handle this https://github.com/laravel/framework/blob/5.2/src/Illuminate/Container/Container.php#L793

[caption id=“attachment_254” align=“aligncenter” width=“300”]laravle-container-getDependencies laravle-container-getDependencies[/caption]

Its not acceptable that throw an error if a non-class cannot resolveable, but its not make sense to make a function name resolveNonClass just only throw an error (imo, should handle this ;)

$dependencies[] = $this->resolveNonClass($parameter);

There is many ways to deal with this is that writing an service provider to inject a string to another class like this (basically, a service provider abstract class contains $app instance, just bind a dependency manually into it). This is one way

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Services\FooService;

class FooProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->bind('App\Services\FooService', function($app){
            return new FooService('a','b');
        });
    }
}

then register above service provider to config/app.php

'provider' => [
    App\Providers\FooProvider::class,
]

its works!!!, other way to bypass this, is use Str provider class instead of built in string primitive php type.

[caption id=“attachment_251” align=“aligncenter” width=“300”]foo service with Str class foo service with Str class[/caption]

By the way, need to update Laravel Container class to deal with primitive type ??? in this function **resolveNonClass **https://github.com/laravel/framework/blob/5.2/src/Illuminate/Container/Container.php#L823

[caption id=“attachment_253” align=“aligncenter” width=“392”]laravel fw container resolveNonClass laravel fw container resolveNonClass[/caption]

Reference: https://laravel.com/docs/5.1/container https://github.com/laravel/framework/blob/5.2/src/Illuminate/Container/Container.php http://culttt.com/2014/03/24/exploring-laravels-ioc-container/ http://php.net/manual/en/class.reflectionclass.php