Follow

PHP question: It looks like it's possible to "use", and therefore alias, a namespaced constant. Is there any way to do the same thing with a namespaced class constant?

@kboyd 3v4l.org/oiM7k
It's not exactly a "use", but it would seem to accomplish what you're looking for.

@kboyd Ignore the eval() part of that, btw. It's just an artifact of trying to do it all in one file.

@pollita Hmm. I guess that's about the same as what I saw (using a temp variable to store the constant's value). Was mostly hoping there was a convenient way to avoid the temp variable.

@pollita Sorry, I didn't provide full context (since it was just some code I was glancing at). I wanted to replace a temporary variable inside a method that was storing a deeply-namespaced class constant's value, with a use statement & alias. The temporary variable only exists because it's more readable than the full reference to the class constant.

@kboyd I thought about making RFC for that as well as for aliasing static methods the way functions are used. AFAIK this is doable and proposed syntax could be `use static const MyClass::CONST_NAME;` & `use static function MyClass::staticMethodName;` maybe I should draft it

Sign in to participate in the conversation
PHP Community on Mastodon

Open source. Open community. We are dedicated to building and enriching the PHP community.