> On Jun 16, 2020, at 3:50 AM, Nikita Popov <nikita....@gmail.com> wrote:
> 
> On Tue, Jun 16, 2020 at 8:59 AM Mike Schinkel <m...@newclarity.net> wrote:
> Hi internals,
> 
> Given that there appears to be some appetite to reduce checks for 
> inappropriate signatures[1] I am wondering if anyone has strong opinions — 
> pro or con — on removing checks for static methods?
> 
> My primary use-case where I would like to see checked relaxed is for static 
> methods used as factory methods[2].
> 
> Per [3] it seems that all but the least upvoted answer argues that LSP 
> applies to instances, not static methods. 
> 
> Per [4] it seems that all upvoted answers agree that constructors should not 
> be constrained by LSP, and since a factory method is a form of a constructor 
> it would seem that if constructors do not require LSP then factory methods 
> would not either.
> 
> Does anyone see any issues with relaxing these checks for static methods that 
> would have them downvote an RFC on the topic?
> 
> Hi Mike,
> 
> The problem here is that static methods signatures are subject to LSP if they 
> are used in conjunction with late static binding (LSB):
> 
> class A {
>     public static function test() {
>         static::method();
>         // May call A::method(), B::method() or any child.
>         // Signature must match for this to be sensible!
>     }
> }
> class B extends A {
> }
> 
> I do agree that the current situation is sub-optimal, because certainly not 
> all static methods actually are used with LSB. But we do not presently 
> distinguish these cases.
> 
> If that problem can be addressed in some way, then I agree that (non-LSB) 
> static methods should be exempt from LSP, just like constructors are.

Thank you for your response Nikita. Much appreciated.

Would it be reasonable to consider moving the check for incompatible signature 
to the first time a method is called using `static::` rather than where the 
method is declared?  That would bypass the need for a new keyword and any 
confusion and BC a new keyword might create.

Also, I think would assume you add need to add a flag to the child methods when 
they are declared so as to know if they match their parent or not?

class A {
    public static function test() {
        static::method();                             // This would work
        $b = B::create(['foo'=>'bar']);          // This would work
        $c = C::create(123,['foo'=>'baz']);   // This would work
        static::create();                                // Error could be 
throw here?
    }
}
class B extends A {
    public static function method() {
    }
    public static function create(array $args) {
    }
}
class C extends B {
    public static function method() {
    }
    public static function create(int$id, array $args) {
    }
}

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

Reply via email to