On Sat, May 13, 2023 at 11:05 AM Lydia de Jongh <flexj...@gmail.com> wrote:
>
> Wow! Yess please. My mail yesterday crossed yours, I guess (thanks Claude
> for answering)
> asking for almost the same:
>
> myClass::$normalProperty::name
>
> So I would like to add my thoughts to your proposal.
>
> I prefer a magic constant like `::name` instead of a function call as it
> can be used in a wider scope; for example as a parameter in an Attribute.
>
> My arguments from yesterday:
>
> > '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.
> >
> >
> Some examples from my mail yesterday for using in attributes:
>
> 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
>
> }
>
> }
>
>
> With a magic constant you could do:
>
> #[UseClass(static::$oSave::name, ImageSave::class)]  // using static for
> normal property?
>
> #[ResetValue($this->isSaved::name, default: true)]  // or using $this ??
> class ImageSaveFactory extends SaveFactoryAbstract {
>
> // negative side-effect: empty classes.....
>
> }
>
>
> From my mail yesterday:
> 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
>
>
> Greetz, Lydia
> P.S. I hope the spacing in de code-example is working now.
>
>
> Op za 13 mei 2023 om 09:27 schreef Robert Landers <landers.rob...@gmail.com
> >:
> >
> > Hello Internals,
> >
> > It is with much trepidation and excitement that I'd like to announce
> > the `nameof` RFC (https://wiki.php.net/rfc/nameof). It has changed
> > quite a bit in the last couple of days, so if you haven't seen the
> > latest draft, please check it out.
> >
> > Essentially, it allows using `nameof()` anywhere a string can be used,
> > even in static contexts. From a developer's perspective, it is a
> > string and from the engine's perspective, it is also mostly a string
> > (depending on how deep we want to go on error checking -- see the
> > RFC).
> >
> > If anything is unclear or if I missed something, please let me know.
> >
> > Robert Landers
> > Software Engineer
> > Utrecht NL
> >
> > --
> > PHP Internals - PHP Runtime Development Mailing List
> > To unsubscribe, visit: https://www.php.net/unsub.php
> >

Hi Lydia,

Please remember to bottom-post responses. But, to respond:

I think the downside of using ::name would be: 'what if I already have
a const called "name"?'

So, we'd have to come up with a different name. Largely, as far as the
compiler would be concerned, we've both got the same idea.

> 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.

I find this particular point an interesting side discussion, as it was
something that bothered me for a while. Ultimately, I ended up with
two solutions:

1. Do no error checking at all with nameof.

This lets the community come up with something that makes sense.
Perhaps someday in the future the community will settle on something
that works well and an RFC will be put forward to add proper error
checking. For example, any of these could work, and all resolve to the
string "normalMethod":

- nameof(ClassName::normalMethod)
- nameof(ClassName::normalMethod(...))
- nameof($_->normalMethod)
- nameof(ClassName->normalMethod(...)) <-- I kinda like this one
- nameof(normalMethod)

2. Emit a warning

This is the one I suspect will be chosen (and I'm starting to like
more and more, after writing out those examples). In this
implementation, we'd emit a warning if you used a name that doesn't
exist. However, PHP has a well-known silence operator (@) that could
be used to silence the warning if you wanted to do something weird
(like one of the examples above). It would still allow an RFC for
referencing instanced properties/methods at a later date, if there is
demand for it. Until then, there is the silence operator.

If there was an RFC for referencing instanced properties or methods,
it could be interesting. Right now, the only way to reference
instanced properties is via reflection, and having this capability
would open the door for having a generic 'declared_type()' (not to be
confused with gettype() that returns the actual type of a variable and
not the declared type) that could instantly return the reflected type.
I can think of a number of applications for something like that... and
all the code I could delete...

It would be magical to write something like:

var_dump(declared_type(ClassName->property));

instead of

$class = new ReflectionClass(ClassName::class);
$property = $class->getProperty('property');
$type = $property->getType();
var_dump($type);

Something to think about...

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to