Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are constants supported in ColdFusion?

I am using ColdFusion 9 and have checked the documentation but it is ambiguous.

https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-language/elements-of-cfml/constants.html

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

Thanks

like image 635
Laszlo Avatar asked Apr 25 '13 16:04

Laszlo


People also ask

What is variables in ColdFusion?

ColdFusion variables can hold many different data types. A data type indicates what kind of information is being stored. ColdFusion variables can store data types, such as numbers, text strings, dates, times, and Boolean values, just to name a few.

What is a constant in programming?

Data values that stay the same every time a program is executed are known as constants. Constants are not expected to change. Literal constants are actual values fixed into the source code . An example of this might be the character string "hello world". The data value "hello world" has been fixed into the code.

How do you check type in ColdFusion?

You can see the current (JVM) type of a variable by doing <cfdump var=#getMetadata(var)# /> or simply by accessing getMetadata(var). getName() .


2 Answers

No, ColdFusion does not have constants. I think in most cases developers just set a variable, using some naming convention such as the variable name in ALL_CAPITALS, and then never change it's value. This is not really a constant as in other languages and you really have to be careful that the value is not changed (because it is not a true constant). I have done this before and typically set these "constants" in the application scope so they are readily available.

There was an enhancement request opened a while back. However, it looks like it has been closed and deferred.

Adam Cameron blogged about this very thing last year and references the same enhancement request.

like image 83
Miguel-F Avatar answered Oct 23 '22 12:10

Miguel-F


No, not as a native language feature. the key bit in the page you linked to is "ColdFusion does not allow you to give names to constants"

I think the page is really talking about literals, rather than constants.

If you want to support unmodifiable constants, I think you'd need to use an object to encapsulate the 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 could then set this in whichever scope you need it (e.g. application or request) then call application.constants.getMailServer()

It's not as concise as the @Miguel-F solution, which is the one I'd use most of the time, but it's here as another option.

like image 27
barnyr Avatar answered Oct 23 '22 11:10

barnyr