1

When binding an implementation for given Interface or Abstract class, e. g

$app->singleton(
    Illuminate\Contracts\Http\Kernel::class,
    App\Http\Kernel::class
);

how laravel determines that given implementation is one of the implementations of the the passed interface/abstract class

Lets assume I have following interfaces

IVehicle.php

namespace App;

interface IVehicle {
    public function getNumberOfWheels();
}

IBicycle.php

namespace App;

interface IBicycle extends IVehicle {

}

I4Wheeler.php

namespace App;

interface I4Wheeler extends IVehicle {

}

Implementations:

Bike.php

namespace App;

class Bike implements IBicycle{

    public function getNumberOfWheels(){
        return 2;
    }
}

Car.php

namespace App;

class Car implements I4Wheeler{

    public function getNumberOfWheels(){
        return 4;
    }
}

Back to bind method :

$app->bind(
    App\IBicycle::class,
    App\Car::class
);

Give the above binding my question is how laravel validates OR not validate that Car is/or not is an implementation of App\IBicycle
interface ? What is the use of passing inteface/abstract class in more general sense if no validation is performed ?

sakhunzai
  • 13,900
  • 23
  • 98
  • 159
  • I think what the Laravel IoC does is: "whenever you need an instance of A, return an instance of B" I don't know if if it internally checks "if B is instanceof A". Have you tried binding a class that does not implement the interface or it does not inherit the specified class? – Glad To Help Oct 28 '15 at 11:47
  • Also, the main purpose of passing interface is code flexibility, hiding the details and dependencies of instantiating concrete implementations. – Glad To Help Oct 28 '15 at 11:53
  • In all honesty, you could have just read the code behind `bind` method which explains clearly what's going on and why (there's a great comment block in `illuminate/container/Container.php`, line 167 - or search for `public function bind(`). – Mjh Oct 28 '15 at 13:45
  • @Mjh , I have been through all Container code , and I did not get that so I am here . Please if you have understanding add answer with some code example , thanks – sakhunzai Oct 28 '15 at 13:49

1 Answers1

0

If I understand your question well, I think you are looking for the Laravel Service Providers. There is where you bind the implementation to an Interface. The IoC will look for those bindings (in your ServiceProviders) to determine which implementation it should use.

Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83