On 19/08/10 09:13, Stas Malyshev wrote:
> Hi!
>
>> I was under the impression that, in order for inheritance to provide
>> proper polymorphism, overridden methods should share the parent's method
>> signature, although they can have additional optional arguments.
>
> Your impression is wrong. Overriden method should have _compatible_
> signature - i.e. accept any argument set that parent object accepted.
> Nothing requires it to have the same signature.
Maybe I didn't express myself clearly: I meant that the arguments in the
child's signature must be a superset of the arguments in the parent
signature, in the same order. Any required arguments in the parent may
be made optional in the child, but not vice versa. The child is free to
accept additional optional arguments, but not exclude any from the parent.

This provides consistency in the same way that (although I haven't
tested in PHP) you should be able to increase the visibility of an
overridden method in a subclass (e.g. protected -> public) but not
decrease it (e.g. protected -> private).

>> Otherwise, if you're passed an ObjChild when you are treating it as an
>> ObjParent, you could get unexpected behaviour if it ignores $param2 when
>> you decide to specify it. PHP doesn't complain about a programmer
>
> Unexpected by whom? Writer of ObjChild->set() knew how to implement it
> without that parameter, so why behavior would be unexpected?
I'm assuming that the parameter would cause variations in behaviour
(which is generally the reason for having parameters), so by definition
ignoring it would cause different behaviour. I presume that this would
be unexpected.

>> passing too many arguments to the function as far as I recall, so this
>> sort of bug can be easy to overlook.
>
> How this is a "bug"?
Arguments being silently ignored sounds like a potential bug to me. I
know it's something I've seen in code I've maintained.

I am prepared to be shown to be wrong on any of these points, though
this is the way I understand things to work from both common sense and
half-remembered OO principles in CompSci.


D

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to