Uses for interfaces?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
Markto
Forum Newbie
Posts: 22
Joined: Thu Nov 17, 2011 1:13 pm

Uses for interfaces?

Post by Markto »

I am having trouble understanding what interfaces are good for. Seems like a lot of work for little payoff. Can someone tell me about a useful case scenario with interfaces, or link me to an article on best practices with interfaces? I am trying to figure out if or why I should use them.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Uses for interfaces?

Post by Christopher »

The short answer is that they enforce the interface definition with the PHP parser and therefore generate error messages when the actual code of the class is different than the defined interface. The are helpful if such interface compliance is important.
(#10850)
Markto
Forum Newbie
Posts: 22
Joined: Thu Nov 17, 2011 1:13 pm

Re: Uses for interfaces?

Post by Markto »

I understand what they do, but I don't understand their use very well. Why would I want to use one? php.net examples are too basic to answer the why question. What is a case in which interface design is important?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Uses for interfaces?

Post by Celauran »

What about code flexibility and dependency injection?

Consider a class that requires database connectivity. Having the constructor require, say, a MySQLi object is no good because it creates tight coupling. What if you use PostgreSQL or Oracle? Abstracting a super class and then extending it into child classes based on database connection types is insane. Your class shouldn't care what type of database connection is being passed in, only that it implements a select few methods. This is where coding against an interface rather than an implementation comes in handy.

What if you're writing a video game (not that you'd do that in PHP, but I digress...) and you need object representations of real world objects? You can throw a baseball or a knife or a water balloon or a chair but there's no sane common parent class for such objects, especially considering single inheritance. They could, however, all implement a Throwable interface.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Uses for interfaces?

Post by Christopher »

Markto wrote:I understand what they do, but I don't understand their use very well. Why would I want to use one?
What they do is their use. If you need what they do, which is generate errors if a class's interface deviates from a defined interface. Obviously this useful if more that one class shares the same interface -- see polymorphism.
Markto wrote:What is a case in which interface design is important?
Interface design, which is the design of the public methods of classes, is very important. But that has nothing to do with PHP's interface syntax, which is simply to have PHP enforce an interface.
(#10850)
Markto
Forum Newbie
Posts: 22
Joined: Thu Nov 17, 2011 1:13 pm

Re: Uses for interfaces?

Post by Markto »

PHP interfaces are still a little bit hazy for me on their usage, but I appreciate the attempts to clear them up. For the example with the Throwable method Celauran mentioned, is there any utility gained in using an interface rather than an abstract class? The abstract class could at least provide some methods for its child classes, but an interface only provides a guarantee for some classes. You seem to get both signatures and some actual methods from abstract classes. I guess the downside is that this only works for child classes of the abstract class, but interfaces can be implemented across many unrelated classes. Is this right or am I missing something?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Uses for interfaces?

Post by Weirdan »

Additionally, a class may implement a number of interfaces, however it can only extend a single abstract class.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Uses for interfaces?

Post by Celauran »

Markto wrote:PHP interfaces are still a little bit hazy for me on their usage, but I appreciate the attempts to clear them up. For the example with the Throwable method Celauran mentioned, is there any utility gained in using an interface rather than an abstract class? The abstract class could at least provide some methods for its child classes, but an interface only provides a guarantee for some classes. You seem to get both signatures and some actual methods from abstract classes. I guess the downside is that this only works for child classes of the abstract class, but interfaces can be implemented across many unrelated classes. Is this right or am I missing something?
That's exactly it. You have single inheritance in PHP but can implement any number of interfaces. (Abstract) super classes are fine when child classes will have mostly similar behaviour, interfaces can be a better choice when you want to ensure a specific behaviour across otherwise unrelated classes.
Markto
Forum Newbie
Posts: 22
Joined: Thu Nov 17, 2011 1:13 pm

Re: Uses for interfaces?

Post by Markto »

Celauran wrote:
Markto wrote:PHP interfaces are still a little bit hazy for me on their usage, but I appreciate the attempts to clear them up. For the example with the Throwable method Celauran mentioned, is there any utility gained in using an interface rather than an abstract class? The abstract class could at least provide some methods for its child classes, but an interface only provides a guarantee for some classes. You seem to get both signatures and some actual methods from abstract classes. I guess the downside is that this only works for child classes of the abstract class, but interfaces can be implemented across many unrelated classes. Is this right or am I missing something?
That's exactly it. You have single inheritance in PHP but can implement any number of interfaces. (Abstract) super classes are fine when child classes will have mostly similar behaviour, interfaces can be a better choice when you want to ensure a specific behaviour across otherwise unrelated classes.
Thanks for clearing that up. The idea is a lot more solid to me now.
jkon
Forum Newbie
Posts: 16
Joined: Mon Feb 15, 2010 6:01 am

Re: Uses for interfaces?

Post by jkon »

Even if you are programming alone and you do all the jobs never think it this way. Think it like you was in a team and you were designing something. Interfaces are something great to put some ground and common rules. For example a vehicle should have gear and a method to set – get them, logical should have a method to change them as well. So this is an interface that a car should implement as well as a bicycle. Some times in PHP I hear the opinion that “I created it … I know what it does … I don’t need ground rules” , that would be correct but … the person that will maintain your code will never be you … maybe your future self … but never you. And even in this evolve you don’t have any reason why making your feature self work harder.
Post Reply