3

I'm not able to get a constant from a class which is defined by using a string variable and PHP 5.3. namespaces. Example:

use \Some\Foo\Bar;

$class = 'Bar';
echo $class::LOCATION;

where LOCATION is a properly defined constant. The error I get says class Bar is undefined.

If I instead do

$class = "\Some\Foo\Bar";

everything works fine.

Is there anyway to make the first example work?

2 Answers 2

4

Using $class::CONST to get a class constant, it is required that $class contains a fully qualified classname independent to the current namespace.

The use statement does not help here, even if you were in the namespace \Some\Foo, the following would not work:

namespace \Some\Foo;

$class = 'Bar';
echo $class::LOCATION;

As you have already written in your question, you have found a "solution" by providing that fully qualified classname:

use \Some\Foo\Bar;

$class = "\Some\Foo\Bar";
echo $class::LOCATION;

However, you dislike this. I don't know your specific problem with it, but generally it looks fine. If you want to resolve the full qualified classname of Bar within your current namespace, you can just instantiate an object of it and access the constant:

use \Some\Foo\Bar;

$class = new Bar;
echo $class::LOCATION;

At this stage you do not even need to care in which namespace you are.

If you however for some reason need to have a class named Bar in the global namespace, you can use class_alias to get it to work. Use it in replace of the use \Some\Foo\Bar to get your desired behaviour:

class_alias('\Some\Foo\Bar', 'Bar');

$class = 'Bar';
echo $class::LOCATION;

But anyway, I might not get your question, because the solution you already have looks fine to me. Maybe you can write what your specific problem is.

Sign up to request clarification or add additional context in comments.

1 Comment

Take, it works for all of the following code, there will always be a class named Bar in the global namespace from the class_alias call ongoing.
0

When you refer to a class name using variables as in $class::MYCONSTANT or $class::myStaticMethod(), PHP assumes that you have given a fully qualified name, i.e., an absolute path to the class. In your case it was \Bar::LOCATION. (The initial backslash is optional here, actually it is deprecated.) Aliases defined by the use keyword are ignored. Thus you get the error that \Bar class is not defined.

The ::class notation (introduced in PHP 5.5) will resolve aliases to the fully qualified names, so Bar::class will be resolved to Some\Foo\Bar including the namespace:

use Some\Foo\Bar;

$class = "Some\Foo\Bar";
// is the same as
$class = Bar::class;

echo $class::LOCATION;

The ::class notation works not only with class names. The phrase static::class stands for the "Late Static Binding" class name given by get_called_class(). Furthermore, if class name does not exist at compile time, it is silently resolved to the current namespace: NonExistent::class is the same as __NAMESPACE__ . '\NonExistent' (or simply 'NonExistent' when not in a namespace). The latter can also be misused to define constants within the current namespace:

namespace Foo;
define (ANGLE_10_PERCENT::class, 180*asin(0.1)/M_PI);
// is the same as
define ('Foo\ANGLE_10_PERCENT', 180*asin(0.1)/M_PI);
// which is approximately 
const ANGLE_10_PERCENT= 11.5;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.