tracker issue : CF-3039641

select a category, or use search below
(searches all categories and all time range)
Title:

Bug 79390:There is currently no way of dynamically invoking a CFC's method in CFScript

| View in Tracker

Status/Resolution/Reason: Closed/Fixed/

Reporter/Name(from Bugbase): Mark Mandel / Mark Mandel (mark.mandel)

Created: 08/11/2009

Components: Language, CFSCRIPT

Versions: 10.0

Failure Type: Unspecified

Found In Build/Fixed In Build: 0000 /

Priority/Frequency: Normal / Unknown

Locale/System: English / Platforms All

Vote Count: 26

Problem:

There is currently no way of dynamically invoking a CFC's method in CFScript.In tags, you are able to use <cfinvoke> to provide this functionality, but if you are in a Script based cfc this is not available.Railo provides a nice way of doing this, in the form of:foo = new Foo();foo[methodName]();Which I think would be a really nice addition to the ColdFusion language, but I would also be happy with a 'invoke' cfscript syntax as well.One thing to note - currently you can pass in the numerically keys arguments into <cfinvoke> as an argumentCollection and it works perfectly.  Please ensure that this continues to work in CFScript, and therefore, this would work in the same way it would for <cfinvoke>args = {1="foo", 2="bar"};foo = new Foo();foo["myMethod"](argumentCollection=args);This is very important for usage with onMissingMethod.

-- Added as per discussion with Adam Cameron/Ray Camden --
The following syntax does seem to work but does not work correctly.
 bar  = foo[method];
 bar();

This happens because the method execution holds the wrong context while executing. The issue is clearly demonstrated by the following example and it needs to be fixed.
C.cfc

{code:java}
component {

    variables.where = "In the CFC!";
    
    public function whereAmI(){
        writeOutput(variables.where & "<br />");
        hidden();
    }
    
    private function hidden(){
        return;    
    }
    
} 
{code}



test.cfm

{code:java}
<cfflush interval="1">
<cfscript>
    variables.where = "In the calling code!";
    
    o = new C();
    
    o.WhereAmI();
    
    methodName = "whereAmI";
    
    methodToCall = o[methodName];
    
    methodToCall();
</cfscript>
{code}



Error is mentioned in the error screen.

Method:


Result:

In the CFC!
In the calling code!

The web site you are accessing has experienced an unexpected error.
Please contact the website administrator.

The following information is meant for the website developer for debugging purposes.
Error Occurred While Processing Request
Variable HIDDEN is undefined.

The error occurred in C:\WebSites\scribble.local\cf\cfcs\pullOut\C.cfc: line 7

5 :    public function whereAmI(){
6 :        writeOutput(variables.where & "<br />");
7 :        hidden();
8 :    }
9 :

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	3039641

External Customer Info:
External Company:  
External Customer Name: Mark Mandel
External Customer Email: 3EA9517D445A9E8999201549
External Test Config: 08/11/2009

Attachments:

Comments:

This bug has been voted..
Vote by External U.
23074 | November 10, 2011 07:01:29 PM GMT
+1 vote. I'd rather the Railo syntax than the invoke() approach, but either would be fine. -- Adam
Vote by External U.
23075 | November 10, 2011 07:01:30 PM GMT
Definitely. But I'd like to see the tag-based version of this implmented even more.
Vote by External U.
23076 | November 10, 2011 07:01:31 PM GMT
This bug has been voted..
Vote by External U.
23077 | November 10, 2011 07:01:32 PM GMT
This bug has been voted..
Vote by External U.
23078 | November 10, 2011 07:01:33 PM GMT
Definitely need to be able to do this in script!
Vote by External U.
23079 | November 10, 2011 07:01:34 PM GMT
This bug has been voted..
Vote by External U.
23080 | November 10, 2011 07:01:35 PM GMT
This bug has been voted..
Vote by External U.
23081 | November 10, 2011 07:01:36 PM GMT
This bug has been voted..
Vote by External U.
23082 | November 10, 2011 07:01:38 PM GMT
This bug has been voted..
Vote by External U.
23083 | November 10, 2011 07:01:39 PM GMT
This would be a great fix.
Vote by External U.
23084 | November 10, 2011 07:01:40 PM GMT
+1, I think this should also work: myVar = new MyCFC("bla")[methodName]("one","two","three");
Vote by External U.
23085 | November 10, 2011 07:01:42 PM GMT
This is a dupe of CF-3033950 which I already voted on so I'm adding a vote to this one as well. The CFML Advisory Committee were unanimous including both Adobe reps that this syntax should be supported: obj[method](arg); Instead you gave us invoke() which is a bad solution for the language.
Vote by External U.
23086 | July 29, 2015 11:11:19 AM GMT
+1 .............................
Vote by External U.
23087 | August 03, 2015 03:21:32 PM GMT
The "solution" of adding invoke() was NOT a fix for this. Back in 2009, both Adobe reps on the CFML Advisory Committee agreed that obj[method](args) should work. Here we are six years later, the community still wants that syntax, and instead we get a silly built-in function which makes it impossible for framework writers to create portable code -- we have to use evaluate() for this! Why can't you just implement the syntax you agreed to back in 2009, that has worked on Railo and OpenBD for YEARS, and works in all other OOP languages that I can think of too. Only the ACF implementation of CFML has this wrong!
Comment by External U.
23063 | August 10, 2015 11:30:42 AM GMT
This is an important addition that has been needed in the language for a LONG time.
Vote by External U.
23088 | August 10, 2015 11:34:05 AM GMT
+1 .............................
Vote by External U.
23089 | August 10, 2015 11:38:51 AM GMT
This is a much needed enhancement and agree that Sean's evaluation of the fix is correct.
Vote by External U.
23090 | August 10, 2015 11:40:27 AM GMT
My CFC's are moving to 100% script syntax and the implementation of a script-based invoke method of some sort would be hugely beneficial.
Vote by External U.
23091 | August 10, 2015 11:45:43 AM GMT
It seems obvious that this should work.
Vote by External U.
23092 | August 10, 2015 11:56:45 AM GMT
+1................................
Vote by External U.
23093 | August 10, 2015 09:17:27 PM GMT
I would like to change my vote from this: {quote} +1 vote. I'd rather the Railo syntax than the invoke() approach, but either would be fine. {quote} To remove the reference to invoke() being fine.
Comment by External U.
23064 | August 11, 2015 12:11:58 AM GMT
In addition to my (itisdesign) +1, I am okay w/ invoke() being removed b/c it was never completed as promised. We were told invoke()'s name would be changed if invoke() wouldn't have all of <cfinvoke>'s functionality. Here we are w/ invoke() being named after <cfinvoke>, but not having all of its functionality. Additionally, cfinvoke() works - but Adobe refuses to support it. Thanks!, -Aaron
Comment by External U.
23065 | August 12, 2015 03:12:55 AM GMT
Is that perhaps grounds for a different ticket, Aaron?
Comment by External U.
23066 | August 12, 2015 03:30:27 AM GMT
Hi Adam, Yes, I agree that should be a separate ticket. I've filed CF-4035747. Thanks!, -Aaron
Comment by External U.
23067 | August 12, 2015 02:30:16 PM GMT
Please explain in 25 characters or more how this feature impacts productivity and why you are adding a vote.
Vote by External U.
23094 | August 13, 2015 10:27:05 AM GMT
So, Adobe. There's been a lot of activity on this ticket recently. Except from you lot. Are you paying attention to what your community is saying here?
Comment by External U.
23068 | October 14, 2015 04:39:15 AM GMT
invoke was added in CF 10 and I am curious to know why it cannot be used for dynamically invoking the CFC methods. Yes, the proposed syntax looks neat but we already have a easy way to achieve the same in script syntax using invoke. We would evaluate supporting the proposed syntax for the next release and we already have a E/R for the same : CF-4035747
Comment by Rupesh K.
23069 | October 14, 2015 07:46:57 AM GMT
Rupesh, the answer to that was already given: read my note below from August 10th. Instead of doing what the community asked for - something Adobe had already agreed, years ago, should work - you willfully created an incompatible, half-assed workaround.
Comment by External U.
23070 | October 14, 2015 08:12:45 AM GMT
I get that Sean. My question though is on - "instead we get a silly built-in function which makes it impossible for framework writers to create portable code -- we have to use evaluate() for this". Why would you need to use evaluate if you are using invoke?
Comment by Rupesh K.
23071 | October 14, 2015 08:47:48 AM GMT
Because invoke() is not portable (across engines or versions). The really frustrating thing is that Adobe didn't implement the obj[method](args) syntax in CF9 -- when everyone agreed it should work, including the two Adobe reps on the CFML Advisory Committee. Then Adobe didn't implement it CF10. And then again Adobe didn't implement it in CF11. When that same syntax worked in the other CFML engines way back before even CF9. So you've punished framework and library writers that have to produce portable code -- instead of doing what you were asked to do and what your own people agreed was the right thing to do. The only portable option we have is evaluate().
Comment by External U.
23072 | October 14, 2015 11:06:00 AM GMT
Please explain in 25 characters or more how this feature impacts productivity and why you are adding a vote.
Vote by External U.
23095 | October 14, 2015 11:18:10 AM GMT
This syntax works in Railo/Lucee and is very helpful for frameworks like ColdBox/TestBox that deal with dynamic method invocation. Please consider this for CF12 as we currently have to use evaluate() to get a portable solution that works on all CF engines. Also, the votes on CF-3033950 should be counted towards this ticket.
Comment by External U.
23073 | October 14, 2015 11:18:26 AM GMT
+1111111111111111111111111
Vote by External U.
23096 | October 15, 2015 09:21:34 AM GMT
Basiclly using CFScript for all components now. This seems like an obvious one.
Vote by External U.
23097 | October 15, 2015 09:35:45 AM GMT
+1111111111111111111111111
Vote by External U.
23098 | October 15, 2015 11:51:13 AM GMT
This is another annoying ommision I'd like to see fixed
Vote by External U.
23099 | October 20, 2015 02:58:41 AM GMT