Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118400 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 74787 invoked from network); 10 Aug 2022 15:27:31 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 Aug 2022 15:27:31 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A5AE21804BC for ; Wed, 10 Aug 2022 10:29:20 -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=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM,FREEMAIL_REPLYTO_END_DIGIT,HTML_MESSAGE, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,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-pf1-f180.google.com (mail-pf1-f180.google.com [209.85.210.180]) (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:29:20 -0700 (PDT) Received: by mail-pf1-f180.google.com with SMTP id k14so12283391pfh.0 for ; Wed, 10 Aug 2022 10:29:20 -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:reply-to:in-reply-to:references :mime-version:from:to:cc; bh=vsIW/KiXI3KV5GEZCsksgvf1aTxGN4bwn2N7SSqUHNo=; b=A+TCOKWPO1m6MdYalWP9+1c9gql2hqjDX/1XMUW7rqC+J5lTpWMYXnHIIXJONk6qVt 8eu2aUTPhsC9PF1TyLCceH7NEYoIVa+rNRMNu4+vHGy4BOFHQBQYtfU0Q2JUcM1QeYCI ChMkzQcUwdaw4Q3RfQ0ZnVPmEi9odMDoaxwO2LktH+0OrodvfTZqfs+PfFfelPjZwJP7 Tp10egogf3AzSL+EDkoEisnwgKa75FPvtUhty3VFIkdog0XfWpqOyk42zUVuO5+Cb/Ew sg3V6GiVVp8RCJTc+VramnhVH3TAhFjEOL4H1twmmGAK6mevAIfAR2wFd739Mz0a929m Wukg== 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:reply-to:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=vsIW/KiXI3KV5GEZCsksgvf1aTxGN4bwn2N7SSqUHNo=; b=B7sZWdwldUYw8eDkVGuyQRcMPS9pr7rJG14FRxAlygc/M+OBjL6PeFKDf7mw61vDdx 9FS9JAXNNzUwRUwx0nGC8oHoxIKAMQo2T954UhKm1LBeg/hwK1k5UvujL2SZLT56Ltm5 DEBKheEroiORR4KyoR1bDqtsUoF7ncXonVX3KQF5TALzmSHK7pVIoyVUqDxtbvvV5dTK ep8KQ36RlJLUP1wFC3tpjllD4kC7OQERbwdJJjb6em1f2JFNPhk8LIMXlj1C7klG4UXP oq4RVM3UFvVrGobZowjsJBZqp0EikrPaGcGruf00hY7hB7C8MGxtg50ExlIGOWPpPWWP nZiA== X-Gm-Message-State: ACgBeo2esGNXpgCqkkMiEycAlHcIYvA4KUmztFyjUK1DG/cSUfBu2vnI WzeguMwpwq++5bUCN596cb0O9HYPuJFX8hWvsrTK7XxEVPl2WFuU87s= X-Google-Smtp-Source: AA6agR7FhdZdjN1NZ9dCBgx34sQ2kP/J97s7MW7c25IEOW8opZxsSmOnqKiakjKbvI8VDJ4dktrVkETZxrXYz9rCT/c= X-Received: by 2002:a05:6a00:148f:b0:52d:4524:8622 with SMTP id v15-20020a056a00148f00b0052d45248622mr28262714pfu.84.1660152559020; Wed, 10 Aug 2022 10:29:19 -0700 (PDT) MIME-Version: 1.0 References: <62f3da72.050a0220.616e8.99a8SMTPIN_ADDED_MISSING@mx.google.com> In-Reply-To: <62f3da72.050a0220.616e8.99a8SMTPIN_ADDED_MISSING@mx.google.com> Reply-To: autaut03@gmail.com Date: Wed, 10 Aug 2022 20:29:08 +0300 Message-ID: To: Ben Ramsey Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary="0000000000008daf2005e5e6636d" Subject: Re: [PHP-DEV] Re: [Concept] Extension methods From: autaut03@gmail.com (Alex Wells) --0000000000008daf2005e5e6636d Content-Type: text/plain; charset="UTF-8" Sorry, replying to all this time :) I've missed to pinpoint an important fact: extensions don't add methods to types per-say, rather they allow using them when imported. Extension's methods would never be called if the extension isn't imported, which is different from monkey-patching and runkit, where two parties could attempt to define the same method twice and end up with only one of them left in the runtime. This also means that it's perfectly fine for two extensions on the same type and with the same method names to co-exist and be used independently. Of course, unless you attempt to import both of them in a scope of one file. On Wed, Aug 10, 2022 at 7:18 PM Ben Ramsey wrote: > On 8/10/22 09:17, Alex Wells wrote: > > > The idea is to introduce extension methods, similar to those in Kotlin, > C#, > > Dart. For those unfamiliar, those are just regular functions with fancy > > syntax. However, I think having those will not only improve readability, > > but also cover some of the previously requested features. > > > Other languages (Kotlin [4], C# [5], Dart [6]) I'm aware of solve this > > problem with extension methods. All of them use slightly different > syntax, > > but the main idea is: > > - you define an extension method the same way you define a function, > except > > you specify which type you're extending > > - you can use any type that a function can accept. This includes > > primitives, classes, interfaces, traits and enums > > - the type you're extending is implicitly bound to `$this` > > - you only have access to the public scope - you can't access > > private/protected members > > - you have to import those the same way you import functions. You can't > > define extensions globally > > 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 > > -- > Cheers, > Ben > --0000000000008daf2005e5e6636d--