Mirage
Mirage

Reputation: 31548

Confused regarding using "Interface" in symfony2 Doctrine

I have this class

UserInterface ---abstract class User implements Userinterface -------- class Teacher extends User

Now i am confused that what type of class i should expect in my php files

I mean in my arguments do i need to type

Class Test(UserInterface $user)

Class Test(User $user)

Class Test (Teacher $user) or Class Test (Teacher $teacher)

I am confused

Upvotes: 0

Views: 122

Answers (1)

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44841

Use the most general one that fits your needs, as it makes your code more flexible. For example, if a method you need to call exists in UserInterface, use it; if, on the other hand, the needed method is defined in User or Teacher, use the corresponding one instead.

Expecting an interface is more flexible than expecting a class, because extending a class is not possible in cases of parallel hierarchies, while you can implement an interface in this case.

Upvotes: 1

Related Questions