C
C
camradee2021-07-01 17:10:32
PHP
camradee, 2021-07-01 17:10:32

How to access a class name alias using a variable?

The question may not be quite right, but that's the gist of it. There are, say, several classes:
use My\SomeClass1 as ClassA;
use My\SomeClass2 as ClassB;
.....
These classes contain constants - ClassA::CONST_A, ClassB::CONST_B and the code would be significantly reduced if it were possible to use a variable as an alias or something like that:
$className = 'ClassA';
$className::CONST_A
Tried with ${...}
Any other options?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-07-01
@poter

I'll upset you boy. You can't use a namespace for dynamic generation. You'll have to write the full path manually.
https://www.php.net/manual/en/language.namespaces....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question