Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118401 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 76187 invoked from network); 10 Aug 2022 15:30:36 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Aug 2022 15:30:36 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A10871804BA for ; Wed, 10 Aug 2022 10:32:26 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-wr1-f53.google.com (mail-wr1-f53.google.com [209.85.221.53]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 10 Aug 2022 10:32:26 -0700 (PDT) Received: by mail-wr1-f53.google.com with SMTP id j7so18571618wrh.3 for ; Wed, 10 Aug 2022 10:32:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=VJIHEdeYji6X0BcqzTjBqXyzCu9/CQYYMy8hP0gB/uk=; b=GLqHpA2rDPrzGmoEQ0wShprvS/Tryf8Uz3DzsM1zAkc5Ne5g67yq24UC6Oj0h9NS5C Tj9BZ0LQgCcHtwsz3R+SDe2cNNJINJcMLVrne8QeTbX5itbTxt100tBEWSL1j3E+uR8X 1xW4Vou2IwX9ZtlFby9OPLuOz1qrsxwu0e9fEVFGJ0iUKUbR0eIqZ6tLSH0MezjXnror +KccrCxS9wFiLpSZhZdW6eT4rP5haEJ0Zn5CKF44cZz4c17Fka4oGpAkb/bUXbPyJjoi bLSR62VM5nGofI/6GcHddvghMJSZe0gZ93MFGxywuKME9gCi/j1ZlJZsPirXCapT6QyG SnkQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=VJIHEdeYji6X0BcqzTjBqXyzCu9/CQYYMy8hP0gB/uk=; b=j+RUO/EwUR7SX2iD8dFCAEfdEjEJdHkjbKdqBKbmbFVDJuchcCPuWvMQMtXW6FiW2l a9B+r0hCy7abwkN5S6+6G6a22H7kB7s+SH0oyp7ZZ9VhKvZE2dPQz/1enBcEmAGQ3Cn3 RkUKQIjnnxkpkuS5XV/atg/d5mQt8uTKCe6uyItUPhBqQ/f/jDWzVSkvzUFJcGx7MIiR KFuQFthEtNaVO6kKxnrlqyBd84TcBCesK4d7RjB+DyaPjeFIIviHjxlpgDMx2Bylkg8O m5rHbPNQrgnJ1dLEp6kiK+slEDuFlzqzlSPBQcJtXQ46C7X45jmUHzVGSb7wyj6mBYpX yOmQ== X-Gm-Message-State: ACgBeo1GYaYIStv9vsrI8tK+qaU+g6caQXoRhI66rPLtG3gKYOmsTaBS 0n2Z8e4SwMine/sY0Q0qQaGyb26QijWEsL/ON3GxiE3eAM8= X-Google-Smtp-Source: AA6agR7bbGQntoog4kOp4ikf19tWjodn+KkL/fhSdXJekfmgRUQnro2vqCD+6PlquZMWS1npjeYzgRRuH399hADDK5c= X-Received: by 2002:a5d:6248:0:b0:222:cd3b:94c8 with SMTP id m8-20020a5d6248000000b00222cd3b94c8mr9773109wrv.97.1660152745141; Wed, 10 Aug 2022 10:32:25 -0700 (PDT) MIME-Version: 1.0 References: <62f3da67.810a0220.ed5e8.1b2cSMTPIN_ADDED_MISSING@mx.google.com> In-Reply-To: <62f3da67.810a0220.ed5e8.1b2cSMTPIN_ADDED_MISSING@mx.google.com> Date: Wed, 10 Aug 2022 18:32:13 +0100 Message-ID: To: Ben Ramsey Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="000000000000a5c2ad05e5e66e2e" Subject: Re: [PHP-DEV] Re: [Concept] Extension methods From: rowan.collins@gmail.com (Rowan Tommins) --000000000000a5c2ad05e5e66e2e Content-Type: text/plain; charset="UTF-8" On Wed, 10 Aug 2022 at 17:18, Ben Ramsey 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); } PHP doesn't know until run-time: * whether App\CollectionExtension actually exists (the compiler does not have access to an autoloader) * whether it defines a "map" method, and applies to type Collection, even if the user has *claimed* that in the "use" statement * what type $x will be; even with a type constraint of "Collection $x", it might be a sub-type, changing the answer to the following question * whether that class already contains a "map" method, or an __call handler I think we would have to implement it as a final fall-back for missing methods, between __call and throwing an error: 1. at compile-time, build a list of in-scope extensions methods; note that these would just be strings at this point, not loaded code 2. just before throwing a "method not found" error, loop over the list, autoloading each entry if necessary; possibly at this point, errors would be raised for naming conflicts and other violations 3. check each in-scope extension method in turn for an "instanceof" match against the current object 4. if one matches, despatch the call 5. if none matches, throw an error as normal Regards, -- Rowan Tommins [IMSoP] --000000000000a5c2ad05e5e66e2e--