Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP interface: specify a parameter list, but not the parameter type

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

like image 893
FMaz008 Avatar asked Oct 18 '25 15:10

FMaz008


1 Answers

"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

like image 103
dantuch Avatar answered Oct 20 '25 07:10

dantuch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!