Mirage
Mirage

Reputation: 31548

Do i need to create interface first in symfony2 before main class

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

Upvotes: 2

Views: 2325

Answers (2)

Steven Mercatante
Steven Mercatante

Reputation: 25305

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 UserInterface is meant to be used so that your user entity will actually work with the rest of the bundle. By agreeing to the contract of UserInterface, 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 to UserInterface, 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.

Upvotes: 4

Codium
Codium

Reputation: 3240

So you can create you own user class that implements this interface. This way you can integrate your class with FOSUserBundle easily

Upvotes: 0

Related Questions