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
Intf.cfc
<cfinterface> <cffunction name = "foo"> </cffunction> </cfinterface>
Comp.cfc
<cfcomponent implements="Intf"> <cffunction name = "foo"> <cfoutput>In method foo</cfoutput> </cffunction> </cfcomponent>
test.cfm
<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 by Troy on March 24th, 2010
| Quote
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:
component path.to.Comp
implements componentsroot.interfaces.Intf
Whereas on others I see this:
component path.to.Comp
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?
#2 by Rupesh Kumar on March 26th, 2010
| Quote
It looks like the code is missing from your comment above.
If you are not able to post it, you can sent that to me at rukumar at adobe dot com
#3 by Troy on March 26th, 2010
| Quote
Ugh, I see that now. Here it is, with square brackets in place of the GT/LT, hoping that gets it through the filters:
[cfset obj = CreateObject("component", "path.to.Comp")/]
[cfdump var="#obj#"/]
However, I will say that we found the problem. We were using a symlink in the path of the mapping, so in this example “path.to.Comp”, the mapping “path” used a symlink in it’s directory path. For whatever reason, Coldfusion would freak out at this and truncate the implements string to just the class name, not the entire package. While this is still irritating, at least I can tell what the difference is in the environments that it was working on and the ones where it wasn’t!
Apparently Adobe released a hotfix for CF that alters the way symlinks are processed. Here’s a blog post talking about how it affects include paths for cfms, not sure if it’s the same problem that affects cfc creation: http://devbox.computec.de/2009/07/coldfusion-8-and-symlinks-hell-awaits/
#4 by системы блэкджек on October 16th, 2010
| Quote
здорово
измените шрифты плохо видно
#5 by сериал дневники вампира 7 серия on November 17th, 2010
| Quote
хорошо ) это да
#6 by картриджи для электронных сигарет gamucci on December 17th, 2010
| Quote
недурно ) это да
#7 by теория механизмов и машин on December 29th, 2010
| Quote
ценно : славно sex forever!
#8 by гипсовые потолочные плиты с п-образным пазом для скрытого монтажа on January 12th, 2011
| Quote
ценно и все таки текст мелковат
#9 by пишу дипломы на заказ on February 1st, 2011
| Quote
клас
клас что есть интернет, такое можно узнать!
#10 by Hektor on April 1st, 2011
| Quote
Конечно тема преподнесена отлично, только можно было бы тему раскрыть больше.
где то на страницах этого блога я уже находил статью на эту тему. Но не помню какая конкретно была страница. Сейчас пойду искать и отпишусь.
#11 by я устал хочу любви клип on August 12th, 2011
| Quote
Сердечное Вам спасибо за Вашу помощь.
#12 by VepfeAmprap on October 26th, 2011
| Quote
Здесь Вы можете расслабиться отдохнуть душой и телеом, все самые мыслимые и не мыслимые удовольствия наши элитные индивидуалки сделают все,
Заходим сюда, звоним http://tinyurl.com/5rw8wxn
Вас ждет масса незабываемых впечатлений и ощущений )))