Namespace constants and use as

Posted by GordonM on Stack Overflow See other posts from Stack Overflow or by GordonM
Published on 2013-11-02T09:35:27Z Indexed on 2013/11/02 9:54 UTC
Read the original article Hit count: 214

Filed under:
|
|
|

I'm having some problems with using constants from a namespace. If I define the constant and try to use as it, PHP seems unable to find it. For example, in my file with the constants I have code along the lines of the following:

namespace \my\namespace\for\constants;

const DS = DIRECTORY_SEPARATOR;

Then in the consuming file I have:

namespace \some\other\namespace;

use \my\namespace\for\constants\DS as DS;

echo (realpath (DS . 'usr' . DS 'local'));

However, instead of echoing '/usr/local' as expected I get the following notice and an empty string.

Notice: Use of undefined constant DS - assumed 'DS'

If I change the code as follows:

use \my\namespace\for\constants as cns;

echo (realpath (cns\DS . 'usr' . cns\DS 'local'));

I get the expected result, but it's obviously quite a bit less convenient than just being able to pull the constants in directly.

You can alias a class/interface/trait in a namespace, are you not able to alias a constant too? If you can do it, then how?

© Stack Overflow or respective owner

Related posts about php

Related posts about namespaces