I am trying to implement my own userBundle and i am am getting ideas from FOSUSerBunle.
WHile i see the code , i notice that he first created the UserInterface and then implements that interface on user entity.
I want to know that what is the use of userInterface , why can’t i directly make UserClass
An interface is an object oriented programming concept. In PHP, it declares (not defines) a set of public methods. Any class that implements an interface is required to define the methods declared within the interface. You can think of an interface as a contract.
Using interfaces allows you to ‘program to an interface‘.
In the case of FOSUserBundle, the
UserInterfaceis meant to be used so that your user entity will actually work with the rest of the bundle. By agreeing to the contract ofUserInterface, your user entity will contain the necessary methods that FOSUserBundle requires of it. Furthermore, you may see type hinting being used within FOSUserBundle that specifically refers toUserInterface, as opposed to a concrete user class.If you’re rolling your own user bundle, you don’t need to implement any interfaces, as the design is completely up to you. But, it sounds like you’re reinventing the wheel here, so I recommend just using FOSUserBundle.