I wanted to create an interface in PHP, but I did not want it to be too strict regarding what type of argument it would accept in one of the public methods. I did not want to do
interface myInterface { public function a( myClass $a); }
Because I do not want to pass an instance of myClass . But I want to make sure that the passed object matches certain parameters that I could fulfill by defining the interface. So I decided to specify classes that use interfaces, for example:
<?php interface iA {} interface iB {} interface iC { public function takes_a( iA $a ); public function takes_b( iB $b ); } class apple implements iA {} class bananna implements iB {} class obj implements iC { public function takes_a( apple $a ) {} public function takes_b( bananna $b ) {} }
But I get an error message PHP Fatal error: Declaration of obj::takes_a() must be compatible with iC::takes_a(iA $a) on line 15
Is there a way to make sure that the argument accepts only the class of a specific interface? Or maybe I'm thinking too much about it?
source share