Re: Mixed mode methods
| From: | Dan Ackroyd | Date: | Mon, 02 Sep 2019 15:01:43 +0000 |
| Subject: | Re: Mixed mode methods | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-106831@lists.php.net to get a copy of this message | ||
On Mon, 2 Sep 2019 at 15:03, Christian Schneider <cschneid@cschneid.com> wrote:
>
> Please don't shoot this down just because you are not the target audience of such a
> feature
That is always good advice.
When you draft the RFC, I strongly recommend coming up with a line of
reasoning of why creating static versions of functions like this:
class Foo {
static function createFromXml($html) {
$instance = new static();
$instance>loadXml();
return $instance;
}
function loadXml($html) {
//...
}
}
Are such a terrible burden, that keeping the mixed mode calling is a
desirable thing. I say that as I think it would need a strong
justification rather than just "it's something that could be done".
In particular, there are tools like
https://github.com/rectorphp/rector that I
understand could be used to
do this automatically, at least for userland code.
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";
}
}
Foo::loadXml();
(new Foo())->loadXml();
// output is
// I am static method
// I am instance method
Although that doesn't meet your goal of allowing seamless upgrades, it
seems like a better approach to allowing that sort of thing in
general. At least in the sense of, if I had to explain this capability
to a junior programmer, explaining that the static method is used when
called statically, and the instance method is used when called on an
instance, would be easier to explain that '$this' might or not be
there.
cheers
Dan
Ack