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

Shared Scope CFC Methods?

Explorer ,
Mar 19, 2008 Mar 19, 2008

Copy link to clipboard

Copied

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.

TOPICS
Advanced techniques

Views

375

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 ,
Mar 19, 2008 Mar 19, 2008

Copy link to clipboard

Copied

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.

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
Valorous Hero ,
Mar 19, 2008 Mar 19, 2008

Copy link to clipboard

Copied

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>

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
Explorer ,
Mar 19, 2008 Mar 19, 2008

Copy link to clipboard

Copied

LATEST
Thanks to both of you. I couldn't have hoped for a better answer.

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