• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

CFCs as an object

Contributor ,
Feb 15, 2007 Feb 15, 2007

Copy link to clipboard

Copied

I have looked for an answer to this but can't find one either way, can I use a CFC like an object? I know I can call it as an object but I am wondering if I can do something like
<cfloop from=1 to=5 index=x>
<cfobject name="myBall#x#" component="aBall">
</cfloop>
Then do something like
<cfset myBall1.setType = "football">
<cfset myBall2.setType = "baseball">

Possible?
TOPICS
Advanced techniques

Views

382

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 15, 2007 Feb 15, 2007

Copy link to clipboard

Copied

What happened when you tried it?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Feb 15, 2007 Feb 15, 2007

Copy link to clipboard

Copied

Look into the CreateObject() method. You should be able to use it like:

<cfset myBall1 = CreateObject("component", aBall)>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Feb 15, 2007 Feb 15, 2007

Copy link to clipboard

Copied

I haven't tried it yet. I decided to find out if anybody else had insight into this before beating my head against the monitor. I was doing some work in .NET and this came to me and I did some research and couldn't find anything about it.

Once I get on my personal development server I can try it.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 15, 2007 Feb 15, 2007

Copy link to clipboard

Copied

Certainly its possible.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2007 Feb 18, 2007

Copy link to clipboard

Copied

Yes, it is possible. Here's an example, where setType is a variable.

<!--- === testPage.cfm === --->
<cfloop from="1" to="3" index="x">
<cfobject name="myBall#x#" component="myCFC1">
</cfloop>
<cfset myBall1.setType = "football">
<cfset myBall2.setType = "baseball">
<cfset myBall3.setType = "basketball">
<cfset myBall1.setPlayer = "Pele"><!--- I know! --->
<cfset myBall2.setPlayer = "Cal">
<cfset myBall3.setPlayer = "Michael">

<cfdump var="#myball1#"><br>
<cfdump var="#myball2#"><br>
<cfdump var="#myball3#">



Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2007 Feb 18, 2007

Copy link to clipboard

Copied

LATEST
It's even possible when you treat a CFC's methods as if they were ordinary variables. An example:

<!--- === testPage2.cfm === --->
<cfloop from="1" to="2" index="x">
<cfobject name="myBall#x#" component="myCFC2">
</cfloop>

<cfset myBall1.setType = myBall1.setSportType>
<cfset myBall1.getType = myBall1.getSportType>
<cfset myBall1.setType("football")>
myBall1.getType(): <cfoutput>#myBall1.getType()#</cfoutput><br>

<cfset myBall2.setType = myBall1.setSportType>
<cfset myBall2.setStarPlayer = myBall1.setPlayer>
<cfset myBall2.getStarPlayer = myBall1.getPlayer>
<cfset myBall2.setType("basketball")>
<cfset myBall2.setStarPlayer()>
myBall2.getStarPlayer(): <cfoutput>#myBall2.getStarPlayer()#</cfoutput>

Now, you're into the realms of the so-called Coldfusion mixins


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation