This is first time I run into overloading method in child class with a different set of parameters than declared in parent class.
Being born as Delphi developer I was pretty sure that this is pretty obvious and possible in PHP like in any other normal programming language. Turned out, I was wrong…
Code works fine, but since I have E_STRICT error reporting mode turned on (and I won’t change it – it saved me a lot of time with pointing out undeclared variables and arrays’ indexes) I’m being bloated with error messages saying for example: “Declaration of ValidationInterface::generateResponse() should be compatible with that of BaseInterface::generateResponse()”.
So, am I getting this correct – to develop webpages with strict accordance to standards I can’t reintroduce (overload) method in child class with different set of attributes than declared in parent class? Or is there any workaround for being able to use this way of coding without issuing above mentioned strict standard warnings?
You can have a “virtual” overload with a different method name, like myMethod() and myMethodEx(). Good for when you can’t change the signature to follow the other option below.
Or you can have the parameters like Gustavo suggested above or like this:
public function myMethod($data, $options = array()) {
// Merge the specified options with the default options.
$options = array_merge(
// The default options.
array(
'option1' => true,
)
,
// The specified options.
$options
);
...
}
Now the overloading method can read the new parameters from the array $options.
Thank you, everyone for an answers. Well… I’ve read carefully (I hope so) everything you wrote here and some other sources of information and if I’m not mistaken, everything talks about properties overloading. There is no reasonable way to overload methods, at least in the way I know it from a different languages (like Delphi).
I know that magic functions in the article cited by mdomba and in some other sources I run into, and some other solutions provided in stackoverflow etc. allows to achieve something similar to method overloading I know, but for many reasons these solutions does not fits me. And the ultimate answer for the question I had – if in PHP strict method overloading, that is if child class can have the same method as parent one, with different set of parameters – is no.
As far as I’m concern error message I’m citing in topic title is only displayed, with E_STRICT error reporting. Many developers is not using it (setting error reporting to E_ALL or E_ERROR). So, since – for many reasons, as I said – I can’t use all this around solutions provided, the only option left is to ignore these kind of messages.
Thanks again for participating in this discussion and sorry for late reply.