Interfaces... require parameter's be defined too?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Son Volt
Forum Newbie
Posts: 19
Joined: Tue Jul 11, 2006 1:36 pm

Interfaces... require parameter's be defined too?

Post by Son Volt »

I know abstract classes require that any methods declared abstract must enforce the parameter list down to any classes derived from this abstract class... Do interfaces also enforce this? I thought interfaces were pure templates. Do these also require the same parameter list? I'm getting an error

Code: Select all

Declaration of EMSTrainingCourse::insert() must be compatible with that of Insert::insert()

What I'm wanting is for the abstract class to enforce all it's derived classes to have an insert() and an update() method but what I don't want to enforce is the parameter list since some classes might have a different number of arguments passe to it's insert() and update() methods. I thought interfaces might be the solution but it's giving me the error above. Any thoughts?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Can you post your class declarations?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The function signature must match. This means the function name, number of arguments and the correct default values (I believe.)
Son Volt
Forum Newbie
Posts: 19
Joined: Tue Jul 11, 2006 1:36 pm

Post by Son Volt »

feyd wrote:The function signature must match. This means the function name, number of arguments and the correct default values (I believe.)

Then why would you use an interface over just declaring and abstract method in your base class?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Off the top of my head:
  1. you can implement many interfaces, but only extend one class.
  2. abstract classes may have implementations in them, while interfaces only contain signatures, no bodies.
Son Volt
Forum Newbie
Posts: 19
Joined: Tue Jul 11, 2006 1:36 pm

Post by Son Volt »

so basically... if I want to enforce a method on all derivied classes, I can only do this with methods that have the same parameter list... this seems strange since the whole point of declaring a method abstract is so you can leave the implementation up to the derived class. Enforcing parameters seems like it goes against this philosophy.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It doesn't to me. The class and its methods must maintain a certain consistency otherwise you could be working with a class that doesn't even work in the expected manners.

The names of the arguments don't need to be the same, just the quantity and default values, otherwise, it isn't compatible with the function it is overwriting.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Exactly. It's about making sure the code that you're working with is going to be interface the way you expect it to.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

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.
Post Reply