0
Callback function in CF?

/t5/coldfusion-discussions/callback-function-in-cf/td-p/134981
Sep 22, 2008
Sep 22, 2008
Copy link to clipboard
Copied
Is there a way to implement a callback function in CF?
The following pseudo callback function works by passing the function's name as a string.
Is there a way to pass a function pointer?
Output of the following code is: in callback function
Thank you in advance.
The following pseudo callback function works by passing the function's name as a string.
Is there a way to pass a function pointer?
Output of the following code is: in callback function
Thank you in advance.
TOPICS
Advanced techniques
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/callback-function-in-cf/m-p/134982#M12577
Sep 22, 2008
Sep 22, 2008
Copy link to clipboard
Copied
> Is there a way to pass a function pointer?
> <cffunction name="func">
> <cfargument name="arg1" type"string">
> <cfoutput>#Evaluate(arg1)#</cfoutput>
> </cffunction>
>
> <cffunction name="callback_func">
> in callback function
> </cffunction>
>
> <cfoutput>
> #func("callback_func()")#<br>
> </cfoutput>
Don't pass the function's name it, pass the function itself in:
{code}
<cffunction name="func">
<cfargument name="arg1" type="any"><!--- not really "any" but nothing more
appropriate to choose from --->
<cfoutput>#arguments.arg1()#</cfoutput>
</cffunction>
<cffunction name="callback_func">
in callback function
</cffunction>
<cfoutput>
#func(arg1=callback_func)#<br>
</cfoutput>
{code}
--
Adam
> <cffunction name="func">
> <cfargument name="arg1" type"string">
> <cfoutput>#Evaluate(arg1)#</cfoutput>
> </cffunction>
>
> <cffunction name="callback_func">
> in callback function
> </cffunction>
>
> <cfoutput>
> #func("callback_func()")#<br>
> </cfoutput>
Don't pass the function's name it, pass the function itself in:
{code}
<cffunction name="func">
<cfargument name="arg1" type="any"><!--- not really "any" but nothing more
appropriate to choose from --->
<cfoutput>#arguments.arg1()#</cfoutput>
</cffunction>
<cffunction name="callback_func">
in callback function
</cffunction>
<cfoutput>
#func(arg1=callback_func)#<br>
</cfoutput>
{code}
--
Adam
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

Guest
AUTHOR
/t5/coldfusion-discussions/callback-function-in-cf/m-p/134987#M12582
Sep 22, 2008
Sep 22, 2008
Copy link to clipboard
Copied
Thanks Adam, that worked real well.
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Valorous Hero
,
/t5/coldfusion-discussions/callback-function-in-cf/m-p/134983#M12578
Sep 22, 2008
Sep 22, 2008
Copy link to clipboard
Copied
Adam Cameron wrote:
> Don't pass the function's name it, pass the function itself in:
Cool. Though it is probably worth noting the callback_function has to be in the same context (ie page or cfc).
> Don't pass the function's name it, pass the function itself in:
Cool. Though it is probably worth noting the callback_function has to be in the same context (ie page or cfc).
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/callback-function-in-cf/m-p/134984#M12579
Sep 22, 2008
Sep 22, 2008
Copy link to clipboard
Copied
> Cool. Though it is probably worth noting the
callback_function has to be in
> the same context (ie page or cfc).
I'm not sure I follow.
<!--- c1.cfc --->
<cfcomponent>
<cffunction name="f">
<cfreturn "This is F">
</cffunction>
</cfcomponent>
<!--- c2.cfc --->
<cfcomponent>
<cffunction name="g">
<cfargument name="func" type="any">
<cfreturn arguments.func()>
</cffunction>
</cfcomponent>
<!--- caller.cfm --->
<cfset copyF = createObject("component", "c1").f>
<cfset o = createObject("component", "c2")>
<cfoutput>#o.g(copyF)#</cfoutput>
This grabs the f() function out of c1, passes it to g() in c2, where it's
executed.
A function (UDF or CFC method) is pretty much just a variable in CF, and
can be passed around as such.
You *would* have a problem if f() referred to other methods or variables
within c1.cfc, because by the time you're running it, it's not the method
that is the member of an instance of C1; it's just "some ol' function". So
anything it *references* must be available in whichever scope it ends up
being.
--
Adam
> the same context (ie page or cfc).
I'm not sure I follow.
<!--- c1.cfc --->
<cfcomponent>
<cffunction name="f">
<cfreturn "This is F">
</cffunction>
</cfcomponent>
<!--- c2.cfc --->
<cfcomponent>
<cffunction name="g">
<cfargument name="func" type="any">
<cfreturn arguments.func()>
</cffunction>
</cfcomponent>
<!--- caller.cfm --->
<cfset copyF = createObject("component", "c1").f>
<cfset o = createObject("component", "c2")>
<cfoutput>#o.g(copyF)#</cfoutput>
This grabs the f() function out of c1, passes it to g() in c2, where it's
executed.
A function (UDF or CFC method) is pretty much just a variable in CF, and
can be passed around as such.
You *would* have a problem if f() referred to other methods or variables
within c1.cfc, because by the time you're running it, it's not the method
that is the member of an instance of C1; it's just "some ol' function". So
anything it *references* must be available in whichever scope it ends up
being.
--
Adam
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
Valorous Hero
,
/t5/coldfusion-discussions/callback-function-in-cf/m-p/134986#M12581
Sep 22, 2008
Sep 22, 2008
Copy link to clipboard
Copied
Adam Cameron wrote:
> You *would* have a problem if f() referred to other methods or variables
> within c1.cfc, because by the time you're running it, it's not the method
> that is the member of an instance of C1; it's just "some ol' function". So
> anything it *references* must be available in whichever scope it ends up
> being.
Yes, that is it exactly. So if c1.cfc contained this code, the call would fail because variables.someProperty is not in scope.
<cfcomponent>
<cfset variables.someProperty = "whatever">
<cffunction name="f">
<cfreturn variables.someProperty>
</cffunction>
</cfcomponent>
> You *would* have a problem if f() referred to other methods or variables
> within c1.cfc, because by the time you're running it, it's not the method
> that is the member of an instance of C1; it's just "some ol' function". So
> anything it *references* must be available in whichever scope it ends up
> being.
Yes, that is it exactly. So if c1.cfc contained this code, the call would fail because variables.someProperty is not in scope.
<cfcomponent>
<cfset variables.someProperty = "whatever">
<cffunction name="f">
<cfreturn variables.someProperty>
</cffunction>
</cfcomponent>
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more
LEGEND
,
/t5/coldfusion-discussions/callback-function-in-cf/m-p/134985#M12580
Sep 22, 2008
Sep 22, 2008
Copy link to clipboard
Copied
> <!--- not really "any" but nothing more
> appropriate to choose from --->
I take that back. Returntype="function" works fine.
--
Adam
> appropriate to choose from --->
I take that back. Returntype="function" works fine.
--
Adam
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting.
Learn more

