Re: Pre proposal for "Class extension functions"

php.internals

Andrew Gromov

7 years ago
I’m thinking a lot about potential problems. Some of them you voiced, but not all. My message was a question of whether the topic was interesting and whether it should be formatted as RFC.
> Bring a little Javascript into PHP?
In fact, I looked at Kotlin :) (https://kotlinlang.org/docs/reference/extensions.html)
> Consider requiring that a class opt in to allowing these methods.
I don't think so. The main point of proposal are to give developers possibility to encapsulate util-functions into classes for calling them more clean. Also I see some others benefits and interesting future capabilities. But I'm not yet ready to talk about it - need to think more.
> If a mixin can suddently access private properties in a foreign class
Yes, it is one of potential problems. Currently I can’t see simply way to avoid private access. But… On the other hand, we have a Reflection. Need to weigh all pros and contras.
> I would make the syntax use the :: delimiter rather than ->
> e.g. function DateTime::localTime() { ... } But that's
'->' - object function, '::' - static function. No need to select only one of them ;)
> The patch doesn't seem to handle inheritance conflicts, though.
This is a very primitive prototype patch for demonstrating how it can work
> For reference: monkey patching methods onto built-in classes is what gives
> you shenanigans like the discussion about `[].smoosh` in Javascript.
One will say "Garbage", and the second "Treasure». When I can do something like this in PHP: function string.explode($a):array { return explode($a, $this); } $a = "Hello world".explode(' '); I'll get drunk with joy :)
> This just opens up a ridiculously wide scope for “we can’t add
> method X because of a BC break in userland code”.
Yes. This is a main argument against. But I have something to counterpose. Extension functions used in many languages. Kotlin, for example, announce them as killer feature (and it is). And there is very little languages with more quivering attitude to a backward compatibility. New functions to internal classes introduce very rare and all that you need - is more accurate migration. Anyway if you try to declare already declared function, then you receive compile time error.

Rasmus Schultz

7 years ago
On Mon, Sep 24, 2018 at 11:24 PM Andrew Gromov <andrewgrom@rambler.ru> wrote:
> > Bring a little Javascript into PHP? > > In fact, I looked at Kotlin :)
Kotlin does not modify classes. This sort of thing is sound in compiled languages like Kotlin and C#, where this is really syntactic sugar for calling user-defined functions with object-like syntax - it's possible (and sound) *only* because these languages can resolve such calls at compile-time. In dynamic languages like JavaScript, you're literally writing to a map of methods, which means execution order is critical, and overwrites are possible - just two reasons why this feature isn't sound in languages like JavaScript, and wouldn't be in PHP either. What's wrong with just calling a function and passing the object as argument? In my opinion, this is the kind of pointless feature that brings nothing new and invites inconsistent code and bikeshedding over pointless details.