Skip to main content

Posts

Showing posts from September, 2009

Concatenating a string to a class constant in PHP

Today I wanted to concatenate a String value to a Class constant to get some task done. I wrote a line as follows.


class DeadlineNotifications extends AppraisalObject {

const ROOT_PATH = '/var/www/dev/nus';
const EMAIL_TEMPLATE_DIR = ROOT_PATH . '/lib/models/performance/emailTemplates/';


Expected value in second constant:
----------------
/var/www/dev/nus/lib/models/performance/emailTemplates/

Actual result:
--------------
Parse error: syntax error, unexpected '.', expecting ',' or ';'


In a way, some one can consider this thing as a bug of PHP. But this is not a bug. You can only initialize class constants with constant values. Here I have passed a statement which has to be evaluated at runtime. But the class constants are defined at compile time which comes earlier.