tags:

views:

118

answers:

2

Here is part of my code

define('DIR_APP', 'app/');

 class Questions
    {


        const QUESTIONS_FILE = DIR_APP . 'questions.xml';
    }

Seems when I take the define()'d constant out of my class constant declaration, it works fine. But when I add it it throws this error:

Parse error: syntax error, unexpected '.', expecting ',' or ';' in /home/public_html/app/classes/Questions.class.php on line 7

So how can I get my define()'d constant in there? I assume it is not correctly looking up the DIR_APP thinking it might of been defined within the class. How do I get it to resolve it globally?

Thank you

A: 

Never tried that before. Zend Studio is giving me an issue at the moment to look at it myself. How about trying this though...

const QUESTIONS_FILE = constant("DIR_APP") . 'questions.xml';
invenetix
It choked on the parenthesis :(
alex
Gah! What version of PHP are you running?
invenetix
php 5 .........................
alex
+2  A: 

It can't be done.

Quote from http://www.php.net/manual/en/language.oop5.constants.php :

"The value must be a constant expression, not (for example) a variable, a class member, result of a mathematical operation or a function call"

I think you could

define('QUESTIONS_FILE', DIR_APP . 'questions.xml');

but that is global.

Spikolynn
That sucks... so I'm left with global defines or normal variables? Has anyone else ran into this problem before?
alex
I'm new to php but I guess having a const as a class member is the same in memory use as having it globally. You could see if that is the case by measuring memory increase when you define all consts outside class def and while defining them in the class, before instantiating it.
Spikolynn