Combining 2 constants in a namespace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Combining 2 constants in a namespace

Post 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
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Combining 2 constants in a namespace

Post 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.
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: Combining 2 constants in a namespace

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: Combining 2 constants in a namespace

Post 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.
Post Reply