【发布时间】:2013-10-25 03:37:33
【问题描述】:
所以在尝试在 Laravel 4 中实现 IoC、DI 等时,我碰壁了。要么是我误解了什么,要么是做错了什么,不知道是哪一个......
所以我有一个类 Person(“业务类”,不是模型或库):
namespace Entities;
use Interfaces\Person as PersonInterface;
class Person implements PersonInterface {...}
一个工厂有:
use Interfaces\Person;
...
App::singleton('user', function($app) {
...
$user_object = new Person();
...
});
在别名数组中:
'Interfaces\Person' => 'Entities\Person'
问题是这不起作用,因为 Person 类无法实现其接口,因为接口绑定回 Person 类:
Entities\Person cannot implement Entities\Person - it is not an interface
我似乎陷入了在应用程序中使用 IoC 和接口阻止类实际实例化的第 22 个问题。
不知道是否相关,但放
App::bind('Interfaces\Person','Entities\Person');
在 routes.php 文件中似乎没有做任何事情(但把它放在 aliases 数组中)。我肯定在这里做错了什么。有什么想法吗?
【问题讨论】:
标签: php interface dependency-injection inversion-of-control laravel-4