View Single Post
Old 10-21-2007, 01:24 AM   #3 (permalink)
sketchMedia
The Gregarious
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 718
Thanks: 29
sketchMedia is on a distinguished road
Default

hehe i didnt get them at first, and i guess my explanation was a bit lacking.

i havnt seen any articles on this site that explain interfaces, my applogies if i have missed them.
Ill have another go at explaining them :D

Basicly the interface is there as a kind of template for any classes that implement it i.e.
PHP Code:
class DBmysql implements DB 
this means that this class must have all the methods listed in the interface. So if i was to code another class, for example for oracle, i would tell it that this new oracle class implements the interface like so
PHP Code:
class DBoracle implements DB 
what this basicly means is that the oracle class must contain everything that is in the interface, it doesnt matter about how the new class works, for example the DBmysql query() method can and will go about sending a query to the database in a different way than the oracle class will, because they are different systems. In this case there is 3 methods stated in the interface:

Our interface looks like this:
PHP Code:
interface DB
{
    public function 
connect();
    public function 
query();
    public function 
disconnect();

now in our new oracle class we must implement each of these methods.
implemented class looks like this:
PHP Code:
class DBoracle implements DB
{
    public function 
connect() { /*do whatever php needs to connect to oracle */ }
    public function 
query(){ /* do oracle query stuff */ }
    public function 
disconnect(){ /* disconnect from oracle */ }

its really away of telling the PHP engine to make sure that those methods are in the implemented class (aswell as making sure that each method has the same visibility i.e. any method with public in the interface must be public within an implemented class of that interface, same applys for parameters, the implemented method must have at least the same number of parameters specified in the interface, it may have more just not less).
Its not totally neccisary but it really helps, especially when you havent coded it yourself, for example someone within the development team (who didnt develop the exsisting system) has been told to extend the system and add support for oracle, instead of reading through all the code finding out the different method names within the exsisting database classes, with an interface he is able to open the interface and find out right away all the method names so he can code the new class accordingly and be 100% sure that when he tries his new oracle class it will work 100% with the rest of the system (as the api will be the same).

To sum it up: Basically, an interface is a contract or a blueprint. When you implement an interface, you must always implement the methods, propeties, and params that it asks of you. In this case it ensures that any new databse connection class will have the same api that the other classes use therefore will have the same api that the rest of the system uses, enabling a smooth transition of database providers, without having to redesign everything again.

i hope i have made things a bit clearer, it confused the hell out of me for a while but it does eventually make sense, especially when you start using it for projects with more than one coder.
__________________
sudo chown -R us ./allyourbase
sketchMedia is offline  
Reply With Quote