Reputation: 2988
Currently i am learning php. Here I have a confusion this is my php code
class OBJECT_ENUM
{
const USER = 10;
const POST = 30;
const SECURE_REQUEST = 40;
}
class OPERATION_ENUM
{
const INSERT_USER = OBJECT_ENUM::USER + 1; // <- here it gives an error
const SEND_MAIL = OBJECT_ENUM::USER + 2;
const LIKE_POST = OBJECT_ENUM::POST + 1;
const INSERT_POST = OBJECT_ENUM::POST + 2;
const ENCRYPT = OBJECT_ENUM::SECURE_REQUEST + 1;
}
error message:
Parse error: syntax error, unexpected '+', expecting ',' or ';' in /var/www/workspace/6thAssignment/include/tempCall.php on line 15
I just don't understand why this error occurs.?? cany anybody explain me.??
Thank you in advance
Upvotes: 2
Views: 1511
Reputation: 13300
ORIGINAL ANSWER:
As you can see in http://www.php.net/manual/en/language.oop5.constants.php:
The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
UPDATED:
From PHP version 5.6 now it is possible to use expressions in constants.
Upvotes: 5
Reputation: 644
At the moment this is not allowed by PHP. There was an RFC (Request for comments) to get this added to the language:
https://wiki.php.net/rfc/const_scalar_expressions
however this was withdrawn as the author of the RFC has left the internals development team. So I suspect this may not happen any time soon but that's not to say that this won't come back in some form or another at a later date.
Upvotes: 0
Reputation: 618
I believe expressions (like $a + 1) are not allowed in constants definitions, so there is why you are getting that error.
Upvotes: 1
Reputation: 7197
I think you can not do mathematical operation to be assigned to a const variable. Try changing
const INSERT_USER = OBJECT_ENUM::USER + 1;
to
$INSERT_USER = OBJECT_ENUM::USER + 1;
Upvotes: 2