Are constants supported in ColdFusion?

I use ColdFusion 9 and checked the documentation, but this is ambiguous.

http://help.adobe.com/en_US/ColdFusion/9.0/Developing/WSc3ff6d0ea77859461172e0811cbec0999c-7ff1.html

(I have a PHP background and search for something similar to PHP constants)

thanks

+7
source share
2 answers

No, ColdFusion has no constants. I think that in most cases, developers simply set the variable using some kind of naming convention, such as the variable name in ALL_CAPITALS, and then never change its value. This is not constant, as in other languages, and you really need to be careful that the value is not changed (because it is not a real constant). I have done this before and usually set these "constants" in the application area so that they are easily accessible.

An extension request appeared, which was opened some time ago. However, it looks like it was closed and postponed.

Adam Cameron spoke about this last year and refers to the same request for improvement.

+19
source

No, not as a function of the native language. the key bit on the page you're linked to is "ColdFusion does not allow you to specify constant names"

I think the page really talks about literals, not about constants.

If you want to support immutable constants, I think you will need to use an object to encapsulate values:

component displayname="constant values for my app" { property name="mailServer" default="127.0.0.1" getter=true setter=false property name="password" default="supersecret" getter=true setter=false } 

You can then install this in any area that you need (for example, an application or request), then call application.constants.getMailServer ()

This is not as concise as the @ Miguel-F solution that I use most of the time, but it is here as another option.

+10
source

All Articles