8 Replies Latest reply on Oct 8, 2009 12:57 PM by Adam Cameron.

    Best place to put cffunctions?

    s.filipowicz Level 1

      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!

        • 1. Re: Best place to put cffunctions?
          Dan Bracuk Level 5

          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.

          1 person found this helpful
          • 2. Re: Best place to put cffunctions?
            s.filipowicz Level 1

            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?

            • 3. Re: Best place to put cffunctions?
              ilssac Level 5

              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.

              • 4. Re: Best place to put cffunctions?
                Dan Bracuk Level 5

                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.

                • 5. Re: Best place to put cffunctions?
                  ilssac Level 5

                  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.

                  • 6. Re: Best place to put cffunctions?
                    Adam Cameron. Level 5

                    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

                    • 7. Re: Best place to put cffunctions?
                      Adam Cameron. Level 5

                      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

                      • 8. Re: Best place to put cffunctions?
                        Adam Cameron. Level 5

                        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