an example of an interface:
Code: Select all
interface iDatabase {
public function __construct ($host, $user, $pass);
public function selectDb($db);
public function connect ();
public function query();
public function getRow();
}
now I can create many database classes for the various RDBMS' out there, but as long as they have methods with the above names and parameters, they can implement the iDatabase interface. Whilst the core functionality of the classes with differ because one will be for MySQL, one for MSSQL, one for PostGRE etc. etc.
This is the beauty of objects.. all my database control is within the relevant database class and as long as those classes implement the above interface, the logic will still complete with minimal fuss.
Now, whilst it may seem 'silly' to use interfaces for an application you are developing, interfaces offer the added assurance that if something is wrong with the class, it will tell me immediately (upon execution)
What I would like to see in future releases of PHP is interfaces specifying the return value, or rather return type of methods as well.
Interfaces also add the 'summary of what a class does' factor.