drsb77336 2015-10-28 11:42
浏览 79

Laravel IoC绑定方法

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 ?

  • 写回答

1条回答 默认 最新

  • drzil26260 2015-10-28 22:18
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画