Page 1 of 1

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

Posted: Thu Jul 27, 2006 10:07 pm
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?

Posted: Thu Jul 27, 2006 10:14 pm
by neophyte
Can you post your class declarations?

Posted: Thu Jul 27, 2006 10:15 pm
by feyd
The function signature must match. This means the function name, number of arguments and the correct default values (I believe.)

Posted: Thu Jul 27, 2006 10:22 pm
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?

Posted: Thu Jul 27, 2006 10:38 pm
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.

Posted: Thu Jul 27, 2006 10:57 pm
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.

Posted: Thu Jul 27, 2006 11:05 pm
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.

Posted: Fri Jul 28, 2006 4:44 am
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.

Posted: Fri Jul 28, 2006 5:01 am
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.