3 Replies Latest reply on Mar 19, 2008 10:10 AM by Rimshot

    Shared Scope CFC Methods?

    Rimshot
      Hi all -

      I just did a cfdump of a cfc I instantiated, and within that dump I see all the methods returned as well as the instance data. This leads me to believe that each instance contains its own copy of the cfc's methods.

      Methods (at least the ones I'm using) within a cfc are going to be exact duplicates from one instantiation to the next, as only the instance data within the CFC is unique per instance, and I'm concerned about the extra overhead/memory required to deliver all methods to all instances. I am wondering if there is a way that the methods can be created just once and shared by each instance. I know I can simply define shared-scope UDF's that each CFC instance can reference externally, but that defeats the benefit of having related code defined in one spot - which is a big benefit of CFC's, and goes against the "best practice" of encapsulation. I also tried to just define methods in the CFC with a shared-scope name, but that causes a CF error. It would be great if there was a way to define a CFC or method to accomplish this.

      Has anyone come up with a good way to do this? Possibly there is a well-known solution of which I am woefully ignorant.

      Any pointers (no pun intended) would be greatly appreciated.

        • 1. Re: Shared Scope CFC Methods?
          Level 7
          Rimshot wrote:
          >
          > Has anyone come up with a good way to do this? Possibly there is a well-known
          > solution of which I am woefully ignorant.
          >
          > Any pointers (no pun intended) would be greatly appreciated.


          It is my understanding that this is how it actually works under the
          hood, no work needed on your part. Yes when you dump the component, all
          the methods are listed there. But the same method in multiple instances
          of the component all use the same code in one memory location, they all
          just get their own pointer to that place.

          I am by know means an authority on this, but that is my understanding
          from my readings.

          • 2. Re: Shared Scope CFC Methods?
            -==cfSearching==- Level 4
            Ian is correct. Instances just contain a pointer, not separate copies of the method. You can test this by creating several instances of a single component and reviewing the hashcode values. They should all be the same.

            <cfset comp1 = createObject("component", "MyComponent").init("a")>
            <cfset comp2 = createObject("component", "MyComponent").init("b")>
            <cfset comp3 = createObject("component", "MyComponent").init("c")>

            <cfoutput>
            #comp1.myFunctionA.hashCode()#<br>
            #comp2.myFunctionA.hashCode()#<br>
            #comp3.myFunctionA.hashCode()#<br>
            </cfoutput>
            • 3. Re: Shared Scope CFC Methods?
              Rimshot Level 1
              Thanks to both of you. I couldn't have hoped for a better answer.