4

I've created an interface called iMapper. And I want all my mappers file to implement that interface.

But each mapper will specify the parameter type.

Example:

interface iMapper
{
    public function insert($obj);
    public function update($obj);
    public function delete($obj);
}
class CarMapper implements iMapper
{
    public function insert(Car $obj){}
    public function update(Car $obj){}
    public function delete(Car $obj){}
}

That code generate the following error:

Declaration of CarMapper::insert() must be compatible with that of iMapper::insert()

Is their a way of making the interface compatible with the CarMapper? (I don't want to change the mapper.)

Thanks

1
  • I made update to my answer :) Commented Apr 20, 2011 at 20:52

4 Answers 4

3

"But each mapper will specify the parameter type." - I have to say that can't be done.

Interface must be implemented. What does it mean? That all implementing classes will have to be able to use methods with not specified parameter - parameter that was required by method inside interface.

calling instanceof inside method body is some kind of way out, but it's realy not a good way.

Read about strategy pattern, I bet it can solve your problem - http://sourcemaking.com/design_patterns/strategy/php

Sign up to request clarification or add additional context in comments.

1 Comment

That sound totally ridiculous to my ears, but as the question was more about the feasibility and not about how the actual behavior is, I think that's the best anwser. Thanks :)
1

better:

interface iObject {}

class Car implements iObject

interface iMapper
{
    public function insert(iObject $obj);
    public function update(iObject $obj);
    public function delete(iObject $obj);
}
class CarMapper implements iMapper
{
    public function insert(Car $obj){}
    public function update(Car $obj){}
    public function delete(Car $obj){}
}

2 Comments

better so that the arguments of the interface methods iMapper can take any object implementing interface iObject (not only the object Car)
Looks tempting but it won't work, because implementation MUST be compatible with declared interface. So instead of CarMapper::insert(Car) it has to be implemented as CarMapper::insert(iObject).
0
interface iMapper
{
    public function insert(Car $obj);
    public function update(Car $obj);
    public function delete(Car $obj);
}
class CarMapper implements iMapper
{
    public function insert(Car $obj){}
    public function update(Car $obj){}
    public function delete(Car $obj){}
}

interface and class methods must match! Same type hinting must be used.

1 Comment

Maybe you're right, but that code doesn't help me much. What's the point of creating 1 interface per object ? I want 1 interface to be use by many objects...
0

Your class has to implement the interface. But it doesn't so PHP complains.

You might use type checking within the methods.

Have a look at instanceof.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.