Skip to main content
July 22, 2008
Answered

Variable scope: Application.cfm vs Application.cfc - very confused

  • July 22, 2008
  • 1 reply
  • 1132 views
Hi all,

ColdFusion newbie here, so please bear with me:)

I'm currently tasked with upgrading a huge ColdFusion 5 project (over 1000 CF pages) to ColdFusion 8, I tried to refactor some of the old code into CFCs, but ran into a problem with variable scope, or the lack thereof.

In the old application, there's one Application.cfm in which a bunch of global variables are defined, without scope, like:
<CFSCRIPT>
...
PrimaryDataSource = "TestDB"
...
</CFSCRIPT>

It seems that variables defined like this can be accessed fine in all the CFM pages using #PrimaryDataSource#, but not in any of my new CFC pages.

What do I have to do to make these global scopeless variables available to the CFCs? Also, what does it actually mean if the global variables are defined without any scope?

I'm really new to ColdFusion, so I'd be very grateful if anyone could shed a light here!
Many thanks in advance.

Billy
This topic has been closed for replies.
Correct answer Newsgroup_User
n3p3nth3 wrote:
> Hi all,
>
> ColdFusion newbie here, so please bear with me:)
>
> I'm currently tasked with upgrading a huge ColdFusion 5 project (over 1000 CF
> pages) to ColdFusion 8, I tried to refactor some of the old code into CFCs, but
> ran into a problem with variable scope, or the lack thereof.
>
> In the old application, there's one Application.cfm in which a bunch of global
> variables are defined, without scope, like:
> <CFSCRIPT>
> ...
> PrimaryDataSource = "TestDB"
> ...
> </CFSCRIPT>
>
> It seems that variables defined like this can be accessed fine in all the CFM
> pages using #PrimaryDataSource#, but not in any of my new CFC pages.
>
> What do I have to do to make these global scopeless variables available to the
> CFCs? Also, what does it actually mean if the global variables are defined
> without any scope?
>
> I'm really new to ColdFusion, so I'd be very grateful if anyone could shed a
> light here!
> Many thanks in advance.
>
> Billy
>

They are not global variables. The are local variables in the variables
scope. On one of your current pages PrimaryDataSource would be the same
as variables.PrimaryDataSource.

How this is currently working is that Application.cfm is an automatic
included file. I.E. For every single request this file is
automatically included at the beginning of the request and all these
local variables are set and only exist for the duration of that request.

When you begin to re-factor the code into CFC's you are going to have to
get a better handle on how to deal with these types of application wide
variables. There are a couple of choices.

One, you can create them as truly global variables in either the
application or session scopes rather then the local, temporary variables
scope. Which you use depends whether the value of a variable is the
same for all users [application] or can be unique to each user [session].

Secondly, you can define these variables inside the CFC itself as a
local 'variables' scope variable inside the CFC. These variables will
live as long as an instance of the CFC lives. Thus if the CFC itself is
persisted in a session or application scope then the 'variables' data
inside of it will persist.

This is a big topic and a single email response is not going to cover
all the ins and outs of it. You are looking at a big task and will want
to do a lot of reading. I suggest starting with the ColdFusion
documentation. It has good chapters that cover all these details
concerning the various variable scopes and other chapters that cover
Components. After the ColdFusion documentation, there is the now three
volume 'ColdFusion Web Application Construction Kit' by Ben Forta et al.
Finally, there are extensive internet blogs, discussion lists and
tutorials that cover all these topics in lesser and greater details.

1 reply

Newsgroup_UserCorrect answer
Inspiring
July 22, 2008
n3p3nth3 wrote:
> Hi all,
>
> ColdFusion newbie here, so please bear with me:)
>
> I'm currently tasked with upgrading a huge ColdFusion 5 project (over 1000 CF
> pages) to ColdFusion 8, I tried to refactor some of the old code into CFCs, but
> ran into a problem with variable scope, or the lack thereof.
>
> In the old application, there's one Application.cfm in which a bunch of global
> variables are defined, without scope, like:
> <CFSCRIPT>
> ...
> PrimaryDataSource = "TestDB"
> ...
> </CFSCRIPT>
>
> It seems that variables defined like this can be accessed fine in all the CFM
> pages using #PrimaryDataSource#, but not in any of my new CFC pages.
>
> What do I have to do to make these global scopeless variables available to the
> CFCs? Also, what does it actually mean if the global variables are defined
> without any scope?
>
> I'm really new to ColdFusion, so I'd be very grateful if anyone could shed a
> light here!
> Many thanks in advance.
>
> Billy
>

They are not global variables. The are local variables in the variables
scope. On one of your current pages PrimaryDataSource would be the same
as variables.PrimaryDataSource.

How this is currently working is that Application.cfm is an automatic
included file. I.E. For every single request this file is
automatically included at the beginning of the request and all these
local variables are set and only exist for the duration of that request.

When you begin to re-factor the code into CFC's you are going to have to
get a better handle on how to deal with these types of application wide
variables. There are a couple of choices.

One, you can create them as truly global variables in either the
application or session scopes rather then the local, temporary variables
scope. Which you use depends whether the value of a variable is the
same for all users [application] or can be unique to each user [session].

Secondly, you can define these variables inside the CFC itself as a
local 'variables' scope variable inside the CFC. These variables will
live as long as an instance of the CFC lives. Thus if the CFC itself is
persisted in a session or application scope then the 'variables' data
inside of it will persist.

This is a big topic and a single email response is not going to cover
all the ins and outs of it. You are looking at a big task and will want
to do a lot of reading. I suggest starting with the ColdFusion
documentation. It has good chapters that cover all these details
concerning the various variable scopes and other chapters that cover
Components. After the ColdFusion documentation, there is the now three
volume 'ColdFusion Web Application Construction Kit' by Ben Forta et al.
Finally, there are extensive internet blogs, discussion lists and
tutorials that cover all these topics in lesser and greater details.
July 22, 2008
Thank you very much for such a detailed explanation, it really helps clarifying things!

Now I am facing a bit of a dilemma: from my heart, I think the "right thing" to do would be refactor all the global variables (they are actually constants) to the application scope where they could easily be accessed by both CFMs and CFCs. But the downside is, I have to change virtually thousands of references to prefix them with "Applicaion.", a pretty scary scenario...

The other way would be to leave the Application.cfm intact and pass the necessary constants to CFC while I'm doing the refactoring, it's not really clean but it does look less risky...

Hm, now that I think about it, maybe it's better to simply double define these variables, both in Application.cfm as local varialbes for the CFMs, and in the Application scope to be accessed by the CFCs?

I know the onApplicationStart in Application.cfc will only be executed once when an application starts, is there some mechanism in the Application.cfm to achieve the same? I want the init code messing with the Application scope to be set-and-forget, but if I simply set it in Application.cfm, it will be executed for every single request, right?