Re: [RFC Proposal] Allow methods to 'become' static
On Sun, 14 Mar 2021 at 12:02, Gert de Pagter <gertp93@gmail.com> wrote:
> Hey Internals,
>
> Recently i've been working on an older code base, where we have a lot
> of classes with all
> static methods. We've been moving to injecting the classes, and
> calling the methods as if they
> were not static.
>
> I wanted to add interfaces to these classes, with non static methods,
> so we can pretend in our implementation they are not static, and allow
> us to easier switch to non static methods in the future.
>
> This is (currently) not allowed by the language: https://3v4l.org/WKdvM
> Is there any chance of this being possible in future versions of PHP?
> This would only allow 'normal' methods to become static. Making static
> methods non static would break Liskov substitution principle in PHP as
> you may not be able to call the method in the same way. This means you
> can't have a 'normal' method become static in an inherited class, and
> then become 'normal' again in another inherited class. This works the
> same way with co/contra-variance in return/parameter types:
> (https://3v4l.org/j1SO9)
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: https://www.php.net/unsub.php
>
>
It's maybe possible in the special case of implementing interfaces, but
it's unsound for general inheritance:
<?php
class A {
public int $someInt = 0;
public function getSomeInt() : int {
return $this->someInt;
}
}
class B extends A {
public static function getSomeInt() : int {
return parent::getSomeInt();
}
}
https://3v4l.org/WnaES
Thread (8 messages)