6 Replies Latest reply on Feb 18, 2007 2:33 AM by BKBK

    CFCs as an object

    Dinghus Level 1
      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">
      Then do something like
      <cfset myBall1.setType = "football">
      <cfset myBall2.setType = "baseball">

        • 1. Re: CFCs as an object
          Dan Bracuk Level 5
          What happened when you tried it?
          • 2. Re: CFCs as an object
            insuractive Level 3
            Look into the CreateObject() method. You should be able to use it like:

            <cfset myBall1 = CreateObject("component", aBall)>
            • 3. Re: CFCs as an object
              Dinghus Level 1
              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.
              • 4. Re: CFCs as an object
                Certainly its possible.
                • 5. Re: CFCs as an object
                  BKBK Adobe Community Professional & MVP
                  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">
                  <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#">

                  • 6. CFCs as an object
                    BKBK Adobe Community Professional & MVP
                    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">

                    <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