Page 1 of 1
Uses for interfaces?
Posted: Wed Jan 22, 2014 8:30 am
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.
Re: Uses for interfaces?
Posted: Wed Jan 22, 2014 12:00 pm
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.
Re: Uses for interfaces?
Posted: Thu Jan 23, 2014 6:54 am
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?
Re: Uses for interfaces?
Posted: Thu Jan 23, 2014 8:29 am
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.
Re: Uses for interfaces?
Posted: Thu Jan 23, 2014 10:52 pm
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.
Re: Uses for interfaces?
Posted: Sat Feb 01, 2014 2:12 pm
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?
Re: Uses for interfaces?
Posted: Sat Feb 01, 2014 4:18 pm
by Weirdan
Additionally, a class may implement a number of interfaces, however it can only extend a single abstract class.
Re: Uses for interfaces?
Posted: Sat Feb 01, 2014 8:03 pm
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.
Re: Uses for interfaces?
Posted: Sun Feb 02, 2014 12:43 pm
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.
Re: Uses for interfaces?
Posted: Wed Feb 05, 2014 7:43 am
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.