我正在尝试实现我自己的 userBundle,并且我从 FOSUSerBunle 获得想法。 当我看到代码时,我注意到他首先创建了 UserInterface,然后在用户实体上实现该接口(interface)。
我想知道 userInterface 有什么用,为什么不能直接制作 UserClass
最佳答案
接口(interface)是一个面向对象的编程概念。在 PHP 中,它声明(而不是定义)一组公共(public)方法。任何实现接口(interface)的类都需要定义接口(interface)中声明的方法。您可以将接口(interface)视为契约。
使用接口(interface)可以让您'program to an interface '.
对于 FOSUserBundle,UserInterface
的目的是让您的用户实体能够真正与 bundle 的其余部分一起工作。通过同意UserInterface
的契约,您的用户实体将包含FOSUserBundle所需的必要方法。此外,您可能会看到 FOSUserBundle 中使用的类型提示专门指的是 UserInterface
,而不是具体的用户类。
如果您正在滚动自己的用户包,则无需实现任何接口(interface),因为设计完全取决于您。但是,听起来您正在重新发明轮子,因此我建议仅使用 FOSUserBundle。
关于php - 我需要在主类之前先在 symfony2 中创建接口(interface)吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11428057/