Nick Nick - 3 months ago 7
PHP Question

PHP - value from variable to constant

I'm learning OOP in PHP and I want to put value from variable to class constant. How can I do that?

This is my code (not working!):

class Dir {

const ROOT = $_SERVER['DOCUMENT_ROOT']."project/";

function __construct() {

}
}


Is there any solution, how to take value from variable, add string and put it to constant - in OOP?

Answer

From the manual page http://www.php.net/manual/en/language.oop5.constants.php you can find that:

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