Interfaces?
Moderator: General Moderators
-
Bruno De Barros
- Forum Commoner
- Posts: 82
- Joined: Mon May 12, 2008 8:41 am
- Location: Ireland
Interfaces?
I just read a topic where Chris Corbyn spoke about interfaces, and I have searched and learned about interfaces before, but I still don't seem to understand their full use.
I mean, it's nice to have like this list of methods that you need to implement, but what is its true use?
It plots out a guideline for all the classes on that branch, like... you can have a Database interface, and all Database Classes (MySQL, PostgreSQL, Oracle, SQLite...) have to use the functions plotted on the interface. But in my previous projects, I don't think interfaces would bring ANY improvements at all.
Does anybody know what is the use of interfaces?
I mean, it's nice to have like this list of methods that you need to implement, but what is its true use?
It plots out a guideline for all the classes on that branch, like... you can have a Database interface, and all Database Classes (MySQL, PostgreSQL, Oracle, SQLite...) have to use the functions plotted on the interface. But in my previous projects, I don't think interfaces would bring ANY improvements at all.
Does anybody know what is the use of interfaces?
Re: Interfaces?
If you write code on your own then they're probably a bit pointless. The idea is that you define an interface so that anyone else writing code that interacts with yours knows what methods are required, and so PHP can check the methods are all there when someone tries to use your object.
I'm currently writing a system to make thumbnails with GD, and I've defined a 'thumbnail' interface to layout exactly what methods are needed in any object that extends the thumbnail class for special image processing effects. If you tried to extend my thumbnail class with an object that doesn't have a generate() method your code would fail.
Essentially it's a simple way of writing an API.
I'm currently writing a system to make thumbnails with GD, and I've defined a 'thumbnail' interface to layout exactly what methods are needed in any object that extends the thumbnail class for special image processing effects. If you tried to extend my thumbnail class with an object that doesn't have a generate() method your code would fail.
Essentially it's a simple way of writing an API.
Re: Interfaces?
Also, you are allowed to declare multiple interfaces (in the opposite of inheriting just one class) - I'm not going to argue of its pros and coins, but it gives you an additional freedom of software development and design 
There are 10 types of people in this world, those who understand binary and those who don't
Re: Interfaces?
Interfaces are used to define where you want an object to heed to a predetermined literal interface.
An example:
An example:
Code: Select all
interface counter {
public function count();
}Code: Select all
class Foo {
/**
* here we *know* that any object passed to this method
* will have the method count() available as it is a requirement
* of the interface
*/
public function fooBar (counter $c) {
$c->count();
}
}- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Interfaces?
I think there are two main uses for interfaces in PHP right now (that may change
). The first is as onion2k describes, to use them to have the parser enforce an API. This can be useful if the API is intricate or for use with a team of developers. The second reason it to use interfaces to help identify an object using reflection. The main place I have seen this is used is for Dependency Injection because as VladSun noted a class can implement many interfaces but only inherit one class. So it is one way to mark the identity of a class.
(#10850)
Re: Interfaces?
An example I always find convincing is a database abstraction class. Say you are using a class for mysql with methods like execute() and getRow(). These are used in all your models. 5 months afterwards you want to try mssql and decide to swap the classes (in your configuration file you change "mysql" to "mssql" and your factory now instantiates a mssql class). If you create this new class the interface will make sure you don't break your system.
class db {
public static class setInstance(dbInterface $database) { /* your stuff */ }
}
class dbInterface {
public function connect();
public function execute($sql);
}
class mysqldb implements dbInterface {
public function connect() { ... }
public function exectute($sql) { ... }
}
class mssqldb implements dbInterface {
public function connect() { ... }
public function exectute($sql) { ... }
}
Doing it this way will not brake whatever uses your db classes because it knows they must have implemented the methods from the interface.
class db {
public static class setInstance(dbInterface $database) { /* your stuff */ }
}
class dbInterface {
public function connect();
public function execute($sql);
}
class mysqldb implements dbInterface {
public function connect() { ... }
public function exectute($sql) { ... }
}
class mssqldb implements dbInterface {
public function connect() { ... }
public function exectute($sql) { ... }
}
Doing it this way will not brake whatever uses your db classes because it knows they must have implemented the methods from the interface.
-
Bruno De Barros
- Forum Commoner
- Posts: 82
- Joined: Mon May 12, 2008 8:41 am
- Location: Ireland
Re: Interfaces?
They seem helpful, for making APIs, but they are not really very useful, at least for now. Although I use OOP, I still don't make an extreme use of PHP 5's new features. I am trying to discover them piece by piece, by developing a framework of my own.
I have method chaining, a query class that then is extended by insert, update, delete, select... And slowly, but steadily, I am getting used - Oh my god! The extensions of the query class can implement a query interface, that defines the functions that all queries must have, like, the function to execute the query, the function to build the query, etc.
Anyway, slowly, but steadily, I am getting used to all the new things in the new PHP.
I have method chaining, a query class that then is extended by insert, update, delete, select... And slowly, but steadily, I am getting used - Oh my god! The extensions of the query class can implement a query interface, that defines the functions that all queries must have, like, the function to execute the query, the function to build the query, etc.
Anyway, slowly, but steadily, I am getting used to all the new things in the new PHP.
-
Bruno De Barros
- Forum Commoner
- Posts: 82
- Joined: Mon May 12, 2008 8:41 am
- Location: Ireland
Re: Interfaces?
Thanks for that link. I'm reading it. It sure sounds really helpful, but as aforementioned, It looks like it's only good for big projects that require collaboration, so it would only suit me when I started to code open source.
Re: Interfaces?
Remember that even if it is a project you alone work on, you still have to work with yourself. Leave any code alone for some time and it looks like someone elses.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Interfaces?
First of all, that article is about Java which is makes the information only generally useful. Second, you need to read the differences between when he says your should "program to interfaces" and when you should use the "interface" construct. Gamma actually makes a good case for implementing abstract classes instead -- unless you are going to guarantee that the interface is immutable -- forever.
You need to balance smart guys making insightful comments against the fact that they are talking about a non-scripting language that is based on some different premises than PHP. The Java community are clawing their way toward Groovy and Scala to escape those premises, just as PHP did long ago.
You need to balance smart guys making insightful comments against the fact that they are talking about a non-scripting language that is based on some different premises than PHP. The Java community are clawing their way toward Groovy and Scala to escape those premises, just as PHP did long ago.
(#10850)
Re: Interfaces?
I think about abstract classes as a kind of interface too. Besides the advantage, a disadvantage of abstract classes is that sometimes you have to read through the code to spot a dependency (eg $this->login($user) { /* check if instance of abstract user class */ })
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Interfaces?
Just like classes define an interface, but the both allow implementation. That is the real distinction between them and an interface.koen.h wrote:I think about abstract classes as a kind of interface too.
I think you are actually talking about Type Hinting there. You can do login(User $user) with any function.koen.h wrote:Besides the advantage, a disadvantage of abstract classes is that sometimes you have to read through the code to spot a dependency (eg $this->login($user) { /* check if instance of abstract user class */ })
(#10850)
Re: Interfaces?
Multiple inheritence is the biggest problem with using abstract classes as interfaces.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: Interfaces?
I've never really found a need for interfaces... I guess maybe I'm doing it wrong. Usually, subclass implementations are very similar, so there's always a convenience protected method or two that should be placed in the abstract class. If I need multiple interfaces, I set up adapters for each of the interfaces to a central object.