Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Best place to put cffunctions?

New Here ,
Oct 08, 2009 Oct 08, 2009

Hi All,

I've created a cffunction which I need to access more or less from every page. Where is the best place to put it?

Can I put it in the Application.cfc? If yes how, and how can I call it from a page?

Thanks!

TOPICS
Advanced techniques
1.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2009 Oct 08, 2009

My suggestion is to write the function in a separate cfc.  Then, in your onSessionStart function, create an object with that cfc.  That allows you to call the function with session.objectname.functionname()

Should you write any more udf's later, put them in the same cfc and everything will work.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Oct 08, 2009 Oct 08, 2009

Hi Dan,

Thanks very much, I love that idea! This will be very usefull.

The function(s) won't change often, would it be an idea to create the object in onApplicationStart?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Oct 08, 2009 Oct 08, 2009

Just realize if you are putting the function into the session scope, each and every user is getting an entire copy of the function in the server's memory.

Depending on the purpose of the function and how you are using it, this may or may not be a lot of memory used up unnecessarily.

If the function is something that each user does not need their very own copy then it might make more sense to put it into the application scope.  Then there would be one copy of the function that all users utilize.  You could, of course, do that in the OnApplicaitonStart() function of the Application.cfc.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2009 Oct 08, 2009

ianskinner wrote:

Just realize if you are putting the function into the session scope, each and every user is getting an entire copy of the function in the server's memory.

Depending on the purpose of the function and how you are using it, this may or may not be a lot of memory used up unnecessarily.

If the function is something that each user does not need their very own copy then it might make more sense to put it into the application scope.  Then there would be one copy of the function that all users utilize.  You could, of course, do that in the OnApplicaitonStart() function of the Application.cfc.

I agree with Ian that Dan's advice in this case is suboptimal.

The only things that should go in the session scope are things that specifically related to the session.  Which is not the case here.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2009 Oct 08, 2009

Regarding:

The function(s) won't change often, would it be an idea to create the object in onApplicationStart?

If you take that approach, you have to restart the application whenever you edit the cfc.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Oct 08, 2009 Oct 08, 2009

Dan Bracuk wrote:

If you take that approach, you have to restart the application whenever you edit the cfc.

Not necessairly.  If you do nothing else, yes.  But it is not that hard to create a mechinism that allows one to replace the object in applciation memory.

Either a flag that is checked or a special update file that is run are a couple of approaches I have used.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2009 Oct 08, 2009

Dan Bracuk wrote:

Regarding:

The function(s) won't change often, would it be an idea to create the object in onApplicationStart?

If you take that approach, you have to restart the application whenever you edit the cfc.

Be that as it may, the same applies for the session scope too.  Also you shouldn't really be editing CFCs on a production box that often anyhow, and it's entirely reasonable to flush any cached files when doing an update anyhow.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Oct 08, 2009 Oct 08, 2009
LATEST

I've created a cffunction which I need to access more or less from every page. Where is the best place to put it?

Can I put it in the Application.cfc? If yes how, and how can I call it from a page?

If it's just one function, then just put it in your Application.cfc.  Provided you have an onRequest handler specified, then all Application.cfc's functions are exposed to the calling template via the variables scope.  So if you have myFunction() defined in Application.cfc, you simply call it as

<cfset a = myFunction()>

If you have a bunch of functions which can be organised into a library, then you could stick 'em in a CFC, instantiate that in onApplicationStart, putting it in the application scope, and perhaps consider creating a variables-scoped reference to it in onRequest, for ease of calling.

--

Adam

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources