Hi Rowan,

śr., 10 sie 2022 o 19:32 Rowan Tommins <rowan.coll...@gmail.com> napisał(a):

> On Wed, 10 Aug 2022 at 17:18, Ben Ramsey <ram...@php.net> wrote:
>
> > I believe this is also called "monkey patching" in some places, and
> > Ruby, Python, and JavaScript all offer some form of object extension
> > similar to this.
> >
> > There is also the PHP runkit extension that provides some of the
> > functionality you've described: https://www.php.net/runkit7
> >
>
>
> Monkey-patching generally refers to the ability to completely "re-open" a
> class, and implement additional behaviour (or even change existing
> behaviour) *with the same privileges as the original definition*.
>
> Extension methods are a much more constrained feature - they don't break
> the class's encapsulation, only provide an extra syntax for operations that
> would already be legal. In C#, for instance, calling
> "foo.someExtensionMethod(bar)" is just syntactic sugar for the static
> method call "SomeClass.someExtensionMethod(foo, bar)", and cannot hide or
> over-ride a real method with the same name.
>
> The challenge in PHP is that so little is resolved at compile-time.
> Adapting the example from the first post:
>
> namespace App\Business;
> use extension App\CollectionExtension::map on Collection;
>
> function foo($x) {
>     $x
>         ->map(fn ($value) => $value + 1)
>         ->map(fn ($value) => $value * 2);
> }
>

This is close to what I was thinking, there might be even more extended map
of methods and aliases similar to parentheses block known from traits use.
But what is my biggest concern is the amount of changes in opcache cuz this
proposal would require storing each Collection class entry patched by a
given extension for each class name - but these things are not really
runtime at all - meaning we may end up with a blocker because namespaces
are not a real thing in PHP.

Cheers,
Michał Marcin Brzuchalski

Reply via email to