Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116879 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 87480 invoked from network); 13 Jan 2022 13:26:58 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 Jan 2022 13:26:58 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CF726180539 for ; Thu, 13 Jan 2022 06:36:26 -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.8 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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-ua1-f44.google.com (mail-ua1-f44.google.com [209.85.222.44]) (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 ; Thu, 13 Jan 2022 06:36:26 -0800 (PST) Received: by mail-ua1-f44.google.com with SMTP id w21so1975859uan.7 for ; Thu, 13 Jan 2022 06:36:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20210112.gappssmtp.com; s=20210112; h=mime-version:from:date:message-id:subject:to; bh=rvjx8JarKrBb6hidyjHKYISStd6e+Om5zpXsdFRWPgU=; b=0fA4t5WZj8gmtz1/LjbKsPI2JaHanY+22D3PabKXu4/ABhjze48ScTDISD50alf952 69rZTy2ZGxYE1anDrXVWbfi1klICHy6Yzs34ZhMtwpx/nMr9i/xW8ZA+Bk8lNS6Z3LE1 HgNKih6rM81EDLNx2IDW45ga859CL6ka7JIkSolDdxO2SHQ9S4kSSilQ+kDZzGwfOivK 1ro+xluEa4S3KB4jZM01jf9yD5+CsZjJPdWx9VXwOakVsCk/P+Y5QwVk7FewdWYZwoEr UlXo6tAFHO2IwKJMxwsG7afW5XbzSLkoH+/1MC8aGmyFOsk+f3G7gdYSfQ0bSRehIqXb 24RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=rvjx8JarKrBb6hidyjHKYISStd6e+Om5zpXsdFRWPgU=; b=Rkb3LhFhO1BukjMY3TygvXVDqgnEh0ICcaD2eeNwXiVlFWPytYOf/4ETTVij3PTbte e5T5bV4ZmyoXyb8jLz+YJWY3n1Ns9dUX5FwbnbCHmK3oj/cAGq8kqTzklSXHWbtNWcOP W2EXQ6la8egeq+4jnlfO/n6aNwli6Qqm35bEtRswE2jmiGwjQprAZFzUnRi/zSfZwJOo SmH34kmSrPwGXrpvK3nPlmvMe2OQmd9WCZCjYjkwgJPQj+XOm+1qG8LHi7OLx+RJoLXM W/k86XSr5sj+lnPqPzbjmbBfQZmoaaF5ML1x6ahyIJ4Dvv71ENXhs0gIdf4yTy80/6Cl 0c9A== X-Gm-Message-State: AOAM5312YCnVx1OEMx0bfKbV5DXtGRVy8wPRIH8EeXsG4Zyu5lh+An+X 0lNxSNFZcxPi6hlSk4ch4+jyrQzl1HEQlzm5l6InkmszmP6qcQ== X-Google-Smtp-Source: ABdhPJxauXPTl+7NswAB8COv8L29lpGzkTeoh+AZ3ZhwV4Ztd+QIfhRN7G1xVXgnxGeCGQrdlQmScnfFgVc/o+NmZmo= X-Received: by 2002:ab0:6848:: with SMTP id a8mr2351192uas.137.1642084585571; Thu, 13 Jan 2022 06:36:25 -0800 (PST) MIME-Version: 1.0 Date: Thu, 13 Jan 2022 15:36:14 +0100 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: Trait constructors / property initialization From: andreas@dqxtech.net (Andreas Hennings) Hello list, I want to bring up a topic that has bothered me whenever I use traits. I don't have a solution proposal for it, yet, unfortunately. I was going to comment in the other thread about traits, but it seems better suited for a new discussion. ---------- Traits allow to share code between classes of different inheritance chains. They can be used instead of composition, or they can be used to help with composition - e.g. a trait can expose functionality from a specific injected dependency. ---------- When using base classes, we can follow a convention to always call the parent constructor. We can even make the properties in the base class private, to fully encapsulate them. But: When using properties in traits, how can I make sure that they are properly initialized in the class constructor? Also, what if I want to provide an init method with specific logic to set that property? How can I make sure that method will be called in the constructor? I found that Psalm can detect "PropertyNotSetInConstructor", which is also applied to properties from traits. But this is not as straightforward as calling a parent constructor. Can and should we provide a language-level solution for this? Or should this be left to static analysis tools and IDEs? Cheers, Andreas