Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120250 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 87739 invoked from network); 12 May 2023 17:49:45 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 12 May 2023 17:49:45 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E3D54180511 for ; Fri, 12 May 2023 10:49:44 -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_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-f51.google.com (mail-ej1-f51.google.com [209.85.218.51]) (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:49:44 -0700 (PDT) Received: by mail-ej1-f51.google.com with SMTP id a640c23a62f3a-956ff2399b1so1942061566b.3 for ; Fri, 12 May 2023 10:49:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683913783; x=1686505783; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=xdmId4Nsicyh1Xu2+USJQoTBa8Ro1cCYC/eFFKK8+ZU=; b=YO5bU36J59IfKWgMcTvZED16TwVQMMAKEIbJ4eWtp/tavsPRZMSG9HVWsQ6lOl6naf wMm2J+cY9ojI5M3l19Bi+0yVbH0qXLvL+ebf0uf2rUg6TvKb146rqYMZmlYzZzAUWPK9 876UD43FTdj1IKdEKoSwb6bSrnASX1oZv3FxyI0A2E3jiMHpPVydzpke1+GiNrNkJEux yv2zWrXLBWOAHSj9MroV/N5eOw1yCwqq+FHKkVVY0xQh2xK0i/y/1yAxEVdEhH1FU7Z3 ew06ggOEgG2+UiQlJmFSY1BjQx5XjODqos938QCOATF9q43iFBEnplS6NwFB17vnxmnv eBlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683913783; x=1686505783; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=xdmId4Nsicyh1Xu2+USJQoTBa8Ro1cCYC/eFFKK8+ZU=; b=BRkQjqlpMyyGuRAoPTnHdKnwaOlbsGrekVaPYdqM7CNnztPS0xm5mz2Jp23iYwXHgP q6TqqGIXm+oX9DjOkNw/5ymlHEfABqIA+EIBjhlPihXXPUOF891fH1VhilGKSolf2WVu efWhYSwo03O33m7F391OcNoVyPFB8bFPVtWT4ztB3ukGTRMQD5Tbwpvwl3RxI8Eztc5Z RklCTAJz9lh7M2mj4q06LzFOvI/2JfoxmBrmptiShq+RNSFGo9Npllkw7Jb9jT74dpbJ Q0T5J0GfaXwgXNtwaaCmgwPKXOemI8kgupS1rVQKMH4ZWb9HdHJgGxrbiNNeKZtIJmL2 bs/w== X-Gm-Message-State: AC+VfDxN4+TGVLrVF1Ln/FWHs/jHvOYhYALr96d+Q50GC2DyCBQNstlw T90Dy/d+8IPEMoUsXQMrMx8= X-Google-Smtp-Source: ACHHUZ7D6arC7vHFbiCPaxiwd9FfxYCWGoey2yx1Rk4PXnwLUeZsKEKsxan0hWoHKCq6HZox6hXQ8Q== X-Received: by 2002:a17:906:6a22:b0:966:3de0:8998 with SMTP id qw34-20020a1709066a2200b009663de08998mr20082180ejc.73.1683913782820; Fri, 12 May 2023 10:49:42 -0700 (PDT) Received: from smtpclient.apple ([89.249.45.14]) by smtp.gmail.com with ESMTPSA id k18-20020a17090632d200b009661f07db93sm5658175ejk.223.2023.05.12.10.49.42 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 May 2023 10:49:42 -0700 (PDT) Message-ID: <30FD8A58-34AA-4709-B58C-E96F980F971A@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_332305FE-4A91-4F41-BD80-08E1BC46ACF7" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.500.231\)) Date: Fri, 12 May 2023 19:49:31 +0200 In-Reply-To: Cc: PHP Developers Mailing List To: Lydia de Jongh References: X-Mailer: Apple Mail (2.3731.500.231) Subject: Re: [PHP-DEV] Proposal: magic constant '::name' for properties and methods etc. like '::class' is for classes/object From: claude.pache@gmail.com (Claude Pache) --Apple-Mail=_332305FE-4A91-4F41-BD80-08E1BC46ACF7 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Le 12 mai 2023 =C3=A0 19:29, Lydia de Jongh a = =C3=A9crit : >=20 > Hi, >=20 > Lately I made a small object initializer/builder with php Attributes. >=20 > '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. >=20 > 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. >=20 > 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. >=20 > I will try to explain with examples of my code. >=20 > abstract class SaveFactoryAbstract { > use ObjectBuilderTrait; >=20 > #[ResetValue] > protected bool $isSaved =3D false; >=20 > #[InitObject] // tells the builder to initialize this property with = the > latest specs provided by (other) attributes > #[ResetObject] > protected ?SaveInterface $oSave =3D null; >=20 > public function __construct(){ > $this->buildMe(); // provided by the trait > } > } >=20 >=20 > 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.... >=20 > #[UseClass('oSave', ImageSave::class)] > #[ResetValue('isSaved', default: true)] > class ImageSaveFactory extends SaveFactoryAbstract { } >=20 >=20 > As workaround I make classes like: >=20 > class oop { // lower character on purpose, for this is not a normal = class >=20 > final public const isSaved =3D 'isSaved'; >=20 > final public const Save =3D 'oSave'; > final private function __construct() {} > } >=20 > 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 { } >=20 > But this is really annoying..... >=20 >=20 > 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 { > } >=20 > This could also extend possibilities with/for variable variables.... > Like: > $this->myProp::name > myClass::$staticProp::name >=20 >=20 > 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. >=20 > outside scope: > myClass::$staticProperty::name > myClass::$normalProperty::name > myClass::normalMethod::name >=20 > 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 >=20 > inside class/object: > $this->normalProperty::name > $this->normalMethod::name > $this::$staticProperty::name > I hope you like the idea! Greetz, Lydia Hi, That looks like the `nameof` operator proposed a few days ago? https://externals.io/message/120173=EF=BF=BC https://externals.io/message/120173 =E2=80=94Claude --Apple-Mail=_332305FE-4A91-4F41-BD80-08E1BC46ACF7 Content-Type: multipart/related; type="text/html"; boundary="Apple-Mail=_FAFABB34-C9F2-4324-8DA0-B37303D3FB80" --Apple-Mail=_FAFABB34-C9F2-4324-8DA0-B37303D3FB80 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

Le 12 = mai 2023 =C3=A0 19:29, Lydia de Jongh <flexjoly@gmail.com> a = =C3=A9crit :

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 =3D = false;

#[InitObject] // tells the builder to initialize this = property with the
latest specs provided by (other) = attributes
#[ResetObject]
protected ?SaveInterface $oSave =3D = 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 =3D 'isSaved';

final public const Save =3D = '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


Hi,

That looks like the `nameof` operator proposed a = few days ago?