Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120249 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 85173 invoked from network); 12 May 2023 17:29:41 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 May 2023 17:29:41 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3AF4D180547 for ; Fri, 12 May 2023 10:29:40 -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=-0.2 required=5.0 tests=BAYES_40,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,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-ej1-f45.google.com (mail-ej1-f45.google.com [209.85.218.45]) (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 ; Fri, 12 May 2023 10:29:39 -0700 (PDT) Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-969f90d71d4so809529066b.3 for ; Fri, 12 May 2023 10:29:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683912578; x=1686504578; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=vwvkNYtmDFPn8agiavyJ6Lv0dNWG21jM5YyaTY/eGoo=; b=G3QpIHhWx4OvWBzOiT8N4KRwN43CUsAuFebREYdjfyqVEPvD9qenQjVV+OTAP9XyoI wmRsz5lX+oeAP3CAn6D7i9vnj5k3hncDN7+DMeA30prk69/tQRP+T64Byv9i8dNkMkcP kkMRLQf+JVPDg7wn4dDrDmYomn7O/awkp8JfhaZUIoVhWgFTGTbayfxitkqTFsitZ9Pq 00dSorG627PSj1UnmW/PDEtUpz5T7n0KfvwR2Bt/AYcfGDcFeFQPvtj18AznTZl6WFtM iiyqDDvQsC9rdG0LDTBeptx9MbulucX2OnwmrH1GHn8oHFVoB4loabQk94MJwq3NKKir GDaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683912578; x=1686504578; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=vwvkNYtmDFPn8agiavyJ6Lv0dNWG21jM5YyaTY/eGoo=; b=Qnsg3rooomwKkp30JPp+RWFI0p3D6Lo+qJQoI6UH/nBU2RCSORMMPpaaC7Wvd5DMDs QoUWp8Owc/O4n6ML7dS0jsl6JrgONZmNFQprYW4t2f0oaCwXASdvoVKRVTldaHykJMLu IxUma5Dd3jqkEObwZmQTq+nwDQMAofQzv4n3p0vzhP032BsLCOKthBOocqopHqAUGIfx WbcaagqqTvTtwnWfwV6MI6dG/FfUELz02Tv/qy+NKz0m9fbFppeWVOrlRSh7fkUikIf1 2gcO0uuhsv+ZJvk0+yN2Kv20w7Zj57yXjXx/pnUeBXZ906nPg7iyYUxv7pWsxJdUwOA8 1svw== X-Gm-Message-State: AC+VfDwD6gUEUbRbNsqeG704Huxw4X4s5gBqvsZWHXI1YeDCLM/rCiFg lyvipW7yyyTMeCoDNgQTeqhBz7yV9CRnTTjESl6CrHH3IRo= X-Google-Smtp-Source: ACHHUZ6PMQe1cHpn7xG75X9D8TqG9oYTv60OyIyRRQjIpX6OlWMegHUPQ2dn5WDRErJenEnHJaQSFhK6Tu2eWagQ44s= X-Received: by 2002:a17:907:2d90:b0:966:40ad:3af1 with SMTP id gt16-20020a1709072d9000b0096640ad3af1mr20529577ejc.1.1683912577857; Fri, 12 May 2023 10:29:37 -0700 (PDT) MIME-Version: 1.0 Date: Fri, 12 May 2023 19:29:11 +0200 Message-ID: To: PHP Developers Mailing List Content-Type: multipart/alternative; boundary="000000000000093ee705fb827384" Subject: Proposal: magic constant '::name' for properties and methods etc. like '::class' is for classes/object From: flexjoly@gmail.com (Lydia de Jongh) --000000000000093ee705fb827384 Content-Type: text/plain; charset="UTF-8" Hi, Lately I made a small object initializer/builder with php Attributes. 'Directly' accessing properties and methods of the class as a parameter in an Attribute is not really possible. You can do that for a class with: \path\to\MyClass::class, but for properties or methods you have to use strings, without recognition in editors. It would be so nice if more items in php could be accessed with a magic constant as you can do with ::class. With magic constant an editor could recognize it and it limits typos etc. and it provides the developer with a list of the properties/methods in scope. At the moment I implemented a kind of workaround by providing simple classes with the property names in class constants. Or add an extra property to a class with a property name that can be used in an attribute. I will try to explain with examples of my code. abstract class SaveFactoryAbstract { use ObjectBuilderTrait; #[ResetValue] protected bool $isSaved = false; #[InitObject] // tells the builder to initialize this property with the latest specs provided by (other) attributes #[ResetObject] protected ?SaveInterface $oSave = null; public function __construct(){ $this->buildMe(); // provided by the trait } } This is straightforward. Until you extend this class and want to set the class for $oSave. The other developer does not know which properties he can declare with an attribute.... #[UseClass('oSave', ImageSave::class)] #[ResetValue('isSaved', default: true)] class ImageSaveFactory extends SaveFactoryAbstract { } As workaround I make classes like: class oop { // lower character on purpose, for this is not a normal class final public const isSaved = 'isSaved'; final public const Save = 'oSave'; final private function __construct() {} } So the extending class can be: This is recognized by the editor and gives the developer some clues. #[UseClass(oop::Save, ImageSave::class)] #[ResetValue(oop::isSaved, default: true)] class ImageSaveFactory extends SaveFactoryAbstract { } But this is really annoying..... It would be much better if we could do something like: #[UseClass(static::$oSave::name, ImageSave::class)] // 'static::' can be used, since the attribute belong to the scope of this class #[ResetValue(static::$isSaved::name, default: true)] class ImageSaveFactory extends SaveFactoryAbstract { } This could also extend possibilities with/for variable variables.... Like: $this->myProp::name myClass::$staticProp::name A potential difficulty is: how to access normal properties/methods.... Maybe just as if they are static? Like parent::normalMethod() is working.... A normal property or method cannot have the same name as its static partner (sadly) so internally I hope it is not a problem. outside scope: myClass::$staticProperty::name myClass::$normalProperty::name myClass::normalMethod::name inside scope: static::$normalProperty::name self::$normalProperty::name // I do not know if 'self::' adds anything for this purpose, except for private properties/methods maybe inside class/object: $this->normalProperty::name $this->normalMethod::name $this::$staticProperty::name I hope you like the idea! Greetz, Lydia --000000000000093ee705fb827384--