Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118652 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 51431 invoked from network); 18 Sep 2022 20:05:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 18 Sep 2022 20:05:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A947E180210 for ; Sun, 18 Sep 2022 13:05:43 -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-vk1-f182.google.com (mail-vk1-f182.google.com [209.85.221.182]) (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 ; Sun, 18 Sep 2022 13:05:43 -0700 (PDT) Received: by mail-vk1-f182.google.com with SMTP id f76so13149272vkf.7 for ; Sun, 18 Sep 2022 13:05:43 -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:subject:date; bh=xHoWRtBi02vwbR9y6xUcPklpLPcn4btSXPW6+VyxG8w=; b=mM9445dppomXpKZ6Fk2JVxvlL/yambdOxwif9MEMN5o5DWJwiavXnrQe9mjg4kLQlq vIKN4LZfvHRjTA50/SO6Lj5A6zskHIhVf0shODfrOdO1mPK82ksArKOxmERR6vuaNIu6 MvCbkrr83S8wGaXxFkF7SEz9Ygk+agX6Ok4JyXRzSzUp9LF6syK4fJdo/ix5bTl5+ALP CKJaegKakKgI7Sv75Sb3+k4idnRCJuLeQGclxtaYObrd8TGZL4cMS1yvk31groS2MxP3 sh6HHdNl4ZyJE0JyLKFvGB+GkdgdZbhIiC4ViE0wKgwgHFkAb42G20+IIRNHfOKl2NoQ hGFw== 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:subject:date; bh=xHoWRtBi02vwbR9y6xUcPklpLPcn4btSXPW6+VyxG8w=; b=Vv1CMLrlducrtgwBG6o6DgH0M+a7UCXD8WmCjDG60t2htV1SxgscWMdpVLVlZjakQd 5RSB9L+4SXcG9uyA6JnSlO0yTZoj7drcVLioldFKZZJrJ7iHVzrJvSezZ8wwZjqXd5v/ ZZxDbYIS4Mk2PgOd951qjr1VaGE3GMDhJbYt9jxdyd9Cwq/cd32DFxPKegxnKR8hwFwn /hGB2Zi+rK4YbMC+LAYKxtL1m7EL0+canGwpQPq24sHt9bzV++CiAElpS6/VrOipVIOg XeFUKL1kmP8/yH4XIfEI6cMebIdGbaCOPdG0eUMi/hs9Bn+cR7krrpTpg8FgTgRw1VdU TapQ== X-Gm-Message-State: ACrzQf0qxNKtVUu9M7+iekhq+nIA/kaX+b0SHxy9dOB3TvSm63WNhzHm 8ST3TVCUShmolOr81ypaNbLOZgLCayfBIJgbXDM= X-Google-Smtp-Source: AMsMyM41wA/wYvnDsbHZmfO1h9SWlpcchPgMk0q5EkzYEDoJXqSu2vEMjscJkCOpJY2Fd1t6rimLK4cMyBtqtzB4d9w= X-Received: by 2002:a1f:e484:0:b0:3a3:356:daa2 with SMTP id b126-20020a1fe484000000b003a30356daa2mr4870215vkh.39.1663531542180; Sun, 18 Sep 2022 13:05:42 -0700 (PDT) MIME-Version: 1.0 References: <67365E50-D383-4B5F-B8E9-95AC2E136223@getmailspring.com> In-Reply-To: <67365E50-D383-4B5F-B8E9-95AC2E136223@getmailspring.com> Date: Sun, 18 Sep 2022 13:05:30 -0700 Message-ID: To: Mohammad Amin Chitgarha Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="000000000000a4f9bb05e8f91ee6" Subject: Re: [PHP-DEV] Specify abstract parent methods for traits From: jordan.ledoux@gmail.com (Jordan LeDoux) --000000000000a4f9bb05e8f91ee6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, Sep 18, 2022 at 4:51 AM Mohammad Amin Chitgarha < machitgarhaa@gmail.com> wrote: > Hi. > > Currently, it's possible that, inside a trait's function, use the parent > method of a class using the trait. This way, not only it's implicitly > supposed the trait is used in a class having a parent, but also the paren= t > class has such a method. It doesn't seem to be a good practice, as stated > in the answers of this question ( > https://softwareengineering.stackexchange.com/questions/371067/should-a-t= rait-refer-to-parent-methods > ). > Also, it's almost impossible to override a parent method (using the same > signature) using a trait. This is useful if you have to inherit multiple > classes from a base class, but also override one (or more) of the base > methods with a common functionality provided by a trait. > There are some workarounds to these problems (specially talking about the > later one), but they have their own disadvantages: > Define a trait function with the same signature but a different name and > use it. The main disadvantage is that it's a different method, and is not > polymorphic. > Do (1), include the trait in the derived class(es), then override the > parent method in all derived classes and manually call the trait function= . > Not perfect because you have to copy the same code for all derived classe= s. > > Stick with parent:: in the trait function. Implicit and not good (e.g. > static analyzers and IDEs cannot help). > > Change the parent class to use traits. This is not always possible, as it > might be someone else's code. > > Ignore this please: copy and paste the method all over the place. > > It's also not possible to use things like insteadof or as when including > the trait. > > Here, I want to propose the concept of explicitly declaring the parent > method. The way to achieve this is to use attributes for trait functions, > e.g. #[Override]. > It has the advantage of not requiring the redeclaration of the parent > method as abstract. However, it should be not allowed to use as clause wh= en > including the trait function, as it's against the definition of overridin= g > a method (i.e. it must have the same name and visibility). > Another method is using a new parent specifier (i.e. abstract parent > public function =E2=80=A6), and is more (?) consistent with the current b= ehaviour. > However, it requires duplicating the parent method signature. > There could be methods using insteadof or as, but they has nothing to do > with the trait itself, and doesn't fix the problem of implicit declaratio= n > of the parent method in the trait. > Thanks, > Mohammad Amin Chitgarha. > I can't quite understand what it is you want to accomplish. To make traits work better with parent classes of the classes they are used in? If so, my answer would be that traits aren't supposed to be used in that way in the first place, so any difficulty in doing so isn't a problem to be fixed. A trait should, in theory, (in my own opinion) be able to be used in any class, regardless of semantic correctness. That is, it should not produce compile time or run time errors related to class structure no matter what class it is used on. If a trait fails that test, it is a misuse of the feature in my opinion, and changes to traits that delay that error reporting are not beneficial in my opinion. This is just trying to use traits to make PHP multiple inheritance, but the PHP object model is fundamentally single inheritance. If you want to move PHP towards multiple inheritance, my preference would be to actually do *that* instead of making traits even more difficult and dangerous to use correctly. Jordan --000000000000a4f9bb05e8f91ee6--