Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding variables inside constants

I'm using constants for my error messages inside the Views, and I want to be able to use variables inside them for easier management.

Most of my constants looks like this:

const SUCCESSFULLY_REGISTERED = "<p class='success'>Registration successful!.</p>";

What I want to do is to use a variable for the css class, so that it's easier to change them all in one place. This is what I've tried:

const SUCCESSFULLY_REGISTERED = "<p class='$this->cssClassSuccess'>Registration successful!.</p><p>Log in with the username and password you registered with.</p>";

By some reason that doesn't work and raises the error message below. Why is that and how can this be done in a way that works?

Parse error: syntax error, unexpected '"' in .../View/RegisterView.php on line 15
like image 935
holyredbeard Avatar asked Jun 28 '26 23:06

holyredbeard


1 Answers

This is not possible if you define a constant.

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

Constant Manual php.net
Constant Expression

like image 95
René Höhle Avatar answered Jun 30 '26 13:06

René Höhle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!