Thursday, August 02, 2007

ColdFusion 8 : IsInstanceOf

If you use lot of CFC inside your ColdFusion application, I am sure you would have come across a situation where you would need to know whether the object is an instance of a particular CFC. This is specially needed when you have components extending other component or you are passing the objects around. ColdFusion 8 introduces a new function IsInstanceOf to do exactly the same. It becomes even more useful after we have interfaces in ColdFusion. And the icing on the cake is that it works even with java objects which means that you can use this function to find out if a particular object is of a particular java class type.

Here is how the function looks.

IsInstanceOf(object, typeName)

where typeName is name of the component/Interface or fully qualified java class name.

It returns 'true' if

  • The object passed is an instance of a component which is same as specified type or inherits it or implements the specified interface. Just to be clear, a component 'A' inherits a component 'B' if A or any of its super component extends 'B'. Similarly a Component 'A' implements an interface 'B' if A or any of its super component, implements interface 'B' or any of the interface that 'A' or its parents implement, extends from the specified interface.
  • The object passed is an instance of a java class (created using cfobject or createObject for java class) which is same as specified class name or inherits the specified class name or implements the specified interface.

Here is an example

<cffunction name = "foo">


<cfcomponent implements="Intf">
<cffunction name = "foo">
<cfoutput>In method foo</cfoutput>


<cfset obj = CreateObject("Component", "Comp")>
<!--- Create a Java object --->
<cfset javaObj = CreateObject("java", "java.lang.StringBuffer")>

<cfoutput>object is of type Comp : #IsInstanceOf(obj, "Comp")#</cfoutput><br>
<cfoutput>object is of type Intf : #IsInstanceOf(obj, "Intf")#</cfoutput><br>

<cfoutput>java object is of type String : #IsInstanceOf(javaobj, "java.lang.String")#</cfoutput><br>
<cfoutput>java object is of type StringBuffer : #IsInstanceOf(javaobj, "java.lang.StringBuffer")#</cfoutput><br>

1 comment:

Troy said...

I know this is an old post, but I have a problem with functionality that relates to this.

In your example, the Intf.cfc and Comp.cfc must live in the same directory, since you are able to use implements="Intf". Handy for an example, of course.

However, I have a component that implements an interface, and uses a coldfusion mapping to reference it like so: implements="componentsroot.interfaces.Intf" where there is a mapping with the logical path /componentsroot that maps to a folder which contains a subfolder interfaces and that is where Intf.cfc lives.

All is well and good, except that if I execute the following lines of code:

on some CF instances (all of these identical in CF version) I would see this:
implements componentsroot.interfaces.Intf
Whereas on others I see this:
implements Intf
Note that the full path is not specified in the implements string. This is a huge problem when type checking, either as part of a cfargument or using isInstanceOf, since CF will not evaluate those two to be the same thing (must be a string match).

Any ideas?