Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define class constant with variable

Tags:

oop

php

I need to define constant with value. I've tried to makes something like this:

class Foo {
const ARTICLES_KEY = $this->config->models->key->category;
}

But php says: syntax error, unexpected T_VARIABLE

I also tried to use static var, but it doesnt work.

How to make such thing properly?

like image 937
ovnia Avatar asked Mar 14 '26 19:03

ovnia


1 Answers

You can't: http://www.php.net/manual/en/language.oop5.constants.php reads

It is possible to define constant values on a per-class basis remaining the same and unchangeable. Constants differ from normal variables in that you don't use the $ symbol to declare or use them.

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

like image 121
Marcin Orlowski Avatar answered Mar 16 '26 09:03

Marcin Orlowski



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!