You use the cfobject
tag or the CreateObject
function to create a named instance of an object. You use other ColdFusion tags, such as cfset
and cfoutput
, to invoke the object's properties and methods.
The following sections provide information about creating and using objects that applies to both COM and CORBA objects. The examples assume a sample object named "obj", and that the object has a property called "Property", and methods called "Method1", "Method2", and "Method3".
You create, or instantiate (create a named instance of) an object in ColdFusion with the cfobject
tag or CreateObject
function. The specific attributes or parameters that you use depend on the type of object you use, and are described in detail in "Creating and using COM objects" and "Creating CORBA objects". The following examples use a cfobject
tag to create a COM object and a CreateObject
function to create a CORBA object:
<cfobject
type="COM" action="Create" name="obj" class="sample.MyObject">
obj = CreateObject("CORBA", "d:\temp\tester.ior", "IOR", "Visibroker")
ColdFusion releases any object created by cfobject
or CreateObject
, or returned by other objects, at the end of the ColdFusion page execution.
Use standard ColdFusion statements to access properties as follows:
cfset
tag, such as the following:
<cfset obj.property = "somevalue">
cfset
tag, such as the following:<cfset value = obj.property>
As shown in this example, you do not use parentheses on the right side of the equation to get a property value.
Object methods usually take zero or more arguments. You send In arguments, whose values are not returned to the caller by value. You send Out and In,Out arguments, whose values are returned to the caller, by reference. Arguments sent by reference usually have their value changed by the object. Some methods have return values, while others might not.
Use the following techniques to call methods:
cfset
tag:
<cfset retVal = obj.Method1()>
<cfset x = 23> <cfset retVal = obj.Method1(x, "a string literal")>
<cfset x = 23> <cfset retVal = obj.Method2("x", "a string literal")> <cfoutput> #x#</cfoutput>
In this example, if the object changes the value of x, it now contains a value other than 23.
ColdFusion supports nested (scoped) object calls. For example, if an object method returns another object, and you must invoke a property or method on that object, you can use the syntax in either of the following examples:
<cfset prop = myObj.X.Property>
<cfset objX = myObj.X>
<cfset prop = objX.Property>