1

is there a historical reason or some other kind of reason for this not to work?

class AClass {

    const CONST = 2;

}

echo AClass::CONST;

I get a parse error:

Parse error: syntax error, unexpected 'CONST' (T_CONST)

Just curiosity.

0

2 Answers 2

5

PHP keywords are case-insensitive, so it can't tell the difference between const CONST and const const. When it parses that code it sees the same keyword twice, which is not the right syntax, and so it gets upset.

Sign up to request clarification or add additional context in comments.

2 Comments

All right so it thinks that there is a constant definition but there's a syntax error.
@user3019105 Right. Remember that compilers split the source into tokens and identify which tokens are keywords before they begin to analyze how the tokens are laid out syntactically, so you can't name a constant after any keyword, not just const. E.g., const CLASS = 2; will cause the same complaint.
1

Not sure of any historical reason, but why would you want to name a constant CONST? It's the same as naming a variable $var.

You should be aiming to make sure that constant and variable names actually mean something. When you look at the code a year down the line will you know what the CONST constant is meant to mean?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.