Page 1 of 1

Combining 2 constants in a namespace

Posted: Thu Dec 04, 2008 5:18 am
by anjanesh
Hi

Using PHP 5.3 alpha2. How do I define the ALPHANUM constant ?

Code: Select all

<?php
namespace z;
 
const NUMBERS = '0123456789';
const LETTERS = 'abcdefghijklmnopqrstuvwxyz';
 
const ALPHANUM = constant(__NAMESPACE__.'::NUMBERS').constant(__NAMESPACE__.'::LETTERS');
?>
Thanks

Re: Combining 2 constants in a namespace

Posted: Thu Dec 04, 2008 5:36 am
by requinix
You're already in that namespace. Simply using NUMBERS and LETTERS should be fine.

If not then you probably have to use the define() function.

Re: Combining 2 constants in a namespace

Posted: Thu Dec 04, 2008 6:09 am
by anjanesh
Neither const ALPHANUM = NUMBERS.LETTERS; nor const ALPHANUM = z::NUMBERS.z::LETTERS; works.
I dont think constants can be defined via concatenation/expression.
http://in2.php.net/manual/en/language.namespaces.definition.php wrote:Although any valid PHP code can be contained within a namespace, only three type of code are affected by namespaces: classes, functions and constants.
A define() in a namespace is in the global scope.

Re: Combining 2 constants in a namespace

Posted: Thu Dec 04, 2008 9:32 am
by RobertGonzalez
I am not sure you can define constants using the const construct setting it to an expression. I could be wrong, but I think the const construct follows the same rules for class properties.