Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116846 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 65354 invoked from network); 7 Jan 2022 12:39:16 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 7 Jan 2022 12:39:16 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 737061804F8 for ; Fri, 7 Jan 2022 05:47:13 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.7 required=5.0 tests=BAYES_00,BODY_8BITS, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS57367 213.189.55.0/24 X-Spam-Virus: No X-Envelope-From: Received: from cache4.mydevil.net (cache4.mydevil.net [213.189.55.195]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 7 Jan 2022 05:47:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=korulczyk.pl; s=devil; h=Content-Transfer-Encoding:Content-Type:In-Reply-To :MIME-Version:Date:Message-ID:From:References:To:Subject:Sender:Reply-To:Cc: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=9OrfKS55i6yviPCD9DbJ4zrR0feauWtv7Y1Ykn0ZEQI=; b=qwCiE/CyNxCRv1aIPDGF4hR3/m J0b9mmBTYmWwx1YAUy1uE1FmUmC2lTmEFrLM76rBX3HPvyfXa9xY35H5h9+b/DxNp09lpFC0HAmfG DX/6kgWsmEQz9pwQj/nnYVwjIZtwL5S5h6AgldbF0BWNnDyzyxxgMTb+4GKOHowaOBm4=; To: internals@lists.php.net References: <1641335738.195767637@f174.i.mail.ru> <5a4aebf8-e592-4517-8930-d18b112ef1fd@www.fastmail.com> <5a99809d-afda-546c-5a11-a4f0f821aa37@korulczyk.pl> <6E50C348-9452-40E1-ACE8-06DED11C9136@gmail.com> <9070564d-e138-81f6-c757-812d8962416e@korulczyk.pl> Message-ID: <23f78a61-a73e-eaa3-861f-75e9bca28485@korulczyk.pl> Date: Fri, 7 Jan 2022 14:47:08 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit X-System-Sender: robert@korulczyk.pl X-System-UID: 1534 Subject: Re: [PHP-DEV] RFC: Trait expects interface From: robert@korulczyk.pl (Robert Korulczyk) > I'm not convinced it does, actually. Consider the following trait: > > trait PropertyCount { >     public function count(): int { >         return count(get_object_vars($this)); >     } > } > > This trait CAN be used to implement the built-in Countable interface, and it might be useful to label it as such; but does it really make sense to say > that classes MUST implement that interface? IMO yes, it does. This simplifies rules for detecting implementation of interfaces in traits, so you don't get "yes and no" answers in that case. I'm not really sure why would you not want this - what is the point of marking method as interface implementation, if it is not reflected by type system in actual execution? If it does not make sense in case of your `PropertyCount`, then do not use this feature. > Even if we put it as a requirement, we can't guarantee that the class will actually use the trait's implementation of the interface, because this > would still be valid: > > class Foo implements Countable { >     private $whatever; > >     use PropertyCount { >         count as getPropertyCount; >     } > >     public function count(): int { >         return 0; >     } > } It works the same for classes and inheritance - if you declare `Foo::count()` directly in class, I can always create `FooBar extends Foo` that overwrites your implementation. But I never heard that someone complain about it, so I'm not sure why this is suddenly a problem for traits. Ensuring that particular implementation is used is not the purpose of interfaces, it never was. Also note that right now method signatures from trait can be changed by class, so PHP won't complain about this: trait T { public function t(string $t): void { } } class C { use T { t as tt; } public function t(): string { return 't'; } } This proposal gives you tools to ensure that class does not mess up with method signatures expected by trait - you can create pairs of trait-interface and require interface in trait. -- Regards, Robert Korulczyk