I am getting a very strange error when I try to scope a private method within a ColdFusion component. I have this very simple ColdFusion component:
<cfcomponent output="false"> <cffunction name="PrivateMethod" access="private" returntype="string" output="false" hint="Returns an updated string."> <!--- Define arguments. ---> <cfargument name="Value" type="string" required="true" /> <!--- Return formatted string. ---> <cfreturn ("<strong>" & ARGUMENTS.Value & "</strong>") /> </cffunction> <cffunction name="CallMethod" access="public" returntype="void" output="false" hint="Invokes the private method."> <cfset VARIABLES.PrivateMethod( Value = "Crazy Bananas!" ) /> <!--- Return out. ---> <cfreturn /> </cffunction> <cffunction name="Debug" access="public" returntype="void" output="true" hint="Dumps out private variables."> <cfdump var="#VARIABLES#" label="VARIABLES Scope Dump" /> <cfabort /> </cffunction> </cfcomponent>
It does absolutely nothing. It has one private method named "PrivateMethod". It has two public methods; one, Debug(), that dumps out the variables scope, and another, CallMethod(), that invokes the private method in the VARIABLES scope using named arguments.
Now, if I call the Debug() method on this ColdFusion component I get:
As you can clearly see, the method PrivateMethod() is clearly within the VARIABLES scope of this component. However, when I try to call CallMethod():
<!--- Create CFC instance. ---> <cfset objTest = CreateObject( "component", "Test" ) /> <!--- Invoke method (which calls private method). ---> <cfset objTest.CallMethod() />
... I get this ColdFusion error:
Cannot invoke method PrivateMethod on an object of type coldfusion.runtime.VariableScope with named arguments. Use ordered arguments instead.
Now, I have two options to fix it here. I can either take out the names arguments to the PrivateMethod() function:
<cfset VARIABLES.PrivateMethod( "Crazy Bananas!" ) />
... and just use ordered arguments (values are assigned to arguments in the same order in which they were passed). Or, I can simply not use the a method scope when invoking the private method and let ColdFusion search for the appropriate method:
<cfset PrivateMethod( Value = "Crazy Bananas!" ) />
Both of the "solutions" will allow the code to execute fine. But, of course, this should not be the case. There is no reason that I can see that I should not be able to invoke a private method using its scope.
Any one have any ideas? Is this a bug? Am I just not seeing something?
Want to use code from this post? Check out the license.