In my experience, things that have different roles or behavior should
be differently named.

I have seen a lot of functions with dynamic return types based on a
mode set in the parameter (e.g. the return type could be string,
string[] or string[][] depending on whether some parameter is NULL or
not). In all cases, the confusion and ambiguity caused by this was far
greater than the supposed / perceived convenience.
Often enough, the increased complexity of control flow branches inside
the function would lead to bugs, which could remain undiscovered for
years.

Maybe the only benefits were to not pollute the global namespace as
much, and to give access to the same static variables for caching.
None of these benefits apply in an object-oriented scenario.

A method body which has to handle and distinguish static and
non-static calls would easily suffer from the same problems.

On Mon, 2 Sep 2019 at 18:03, Christian Schneider <cschn...@cschneid.com> wrote:
>
> Am 02.09.2019 um 17:01 schrieb Dan Ackroyd <dan...@basereality.com>:
> > Also(, without checking to see if it's feasible,) to me a less
> > surprising approach would be to allow static and instance methods to
> > be declared separately with the same method name.

This would at least solve the problem of added complexity in the method body.
Still, as Dan Ackroyd points out, there needs to be a clear benefit.


> I was considering this approach but that is a special case of Ad hoc 
> polymorphism (https://en.wikipedia.org/wiki/Ad_hoc_polymorphism).
> And that's a path I don't want to go down, I don't think that's a good fit 
> for PHP.
> It felt more natural to model it similarly to nullable return types.

The value of parameter-based polymorphism is for cases where the
calling code wants to handle all parameter types in one call, whereas
the callee has different implementations for different parameter
types.

This is not the case here: The calling code does always know whether a
call should be static or not, so it can choose the applicable method
name.


On Mon, 2 Sep 2019 at 18:03, Christian Schneider <cschn...@cschneid.com> wrote:
>
> Am 02.09.2019 um 17:01 schrieb Dan Ackroyd <dan...@basereality.com>:
> > Also(, without checking to see if it's feasible,) to me a less
> > surprising approach would be to allow static and instance methods to
> > be declared separately with the same method name.
> >
> > class Foo {
> >    static function loadXml() {
> >        echo "I am static method\n";
> >    }
> >
> >    function loadXml() {
> >        echo "I am instance method\n";
> >    }
> > }
>
> I was considering this approach but that is a special case of Ad hoc 
> polymorphism (https://en.wikipedia.org/wiki/Ad_hoc_polymorphism).
> And that's a path I don't want to go down, I don't think that's a good fit 
> for PHP.
> It felt more natural to model it similarly to nullable return types.
>
> - Chris
>
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>

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

Reply via email to