You use the cfobject
tag to create an instance of an object. You use other ColdFusion tags, such as cfset
and cfoutput
, to invoke properties (attributes), and methods (operations) on the object. An object created by cfobject
or returned by other objects is implicitly released at the end of the ColdFusion page execution.
The examples in the following sections assume that the name
attribute in the cfobject
tag specified the value "obj", and that the object has a property called "Property", and methods called "Method1", "Method2", and "Method3".
Use the following coding practices to access properties.
<cfset obj.property = "somevalue">
<cfset value = obj.property>
Note that parentheses are not used on the right side of the equation for property-gets.
Object methods usually take zero or more arguments. Arguments can be sent by value ([in] arguments) or by reference ([out] and [in,out]). Arguments sent by reference usually have their value changed by the object. Some methods return values, while others may not.
<cfset retVal = obj.Method1()>
Note that parentheses are required for methods with no arguments.
<cfset x = 23> <cfset retVal = obj.Method1(x, "a string literal")>
This method accepts one integer argument and one string argument.
<cfset x = 23> <cfset retVal = obj.Method2("x", "a string literal")> <cfoutput> #x#</cfoutput>
Note the use of double-quotation marks (") to specify reference arguments. If the object changes the value of "x", it now contains a value other than 23.
The current release of ColdFusion does not support nested (scoped) object calls. For example, if an object method returns another object and you must invoke a property or method on that object, use the following syntax:
<cfset objX = myObj.X> <cfset prop = objX.Property>