What Happens When I Return The SUPER Scope From Component Constructor?

Posted September 23, 2008 at 6:39 PM by Ben Nadel

Tags: ColdFusion

There's really no need to ever do this (that I can think of), but the other day it popped into my head - what happens if I return the SUPER scope from a components Init() method? To test this, I set up a base class:

  • <cfcomponent
  • output="false"
  • hint="I am the base component.">
  •  
  • <cffunction
  • name="Foo"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I am the FOO method as defined in the Base class.">
  •  
  • <cfreturn "Base &gt; FOO" />
  • </cffunction>
  •  
  • </cfcomponent>

Then, I created a concrete, extending class that both overrides the Foo() method in the base class as well as providing a new method, Bar():

  • <cfcomponent
  • extends="Base"
  • output="false"
  • hint="I am the concrete class.">
  •  
  •  
  • <cffunction
  • name="Init"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I return an intialized object.">
  •  
  • <!--- Return the SUPER object. --->
  • <cfreturn SUPER />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="Bar"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I am the BAR method as defined in the Concrente class.">
  •  
  • <cfreturn "Concrete &gt; BAR" />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="Foo"
  • access="public"
  • returntype="any"
  • output="false"
  • hint="I am the FOO method as overridden in the Concrente class.">
  •  
  • <cfreturn "Concrete &gt; FOO" />
  • </cffunction>
  •  
  • </cfcomponent>

Notice that in the concrete class, the Init() method returns the SUPER class rather than the traditional THIS reference.

Now, let me test the use of and existence of the various methods:

  • <!--- Create the component and initialize. --->
  • <cfset objConcrete = CreateObject( "component", "Concrete" )
  • .Init()
  • />
  •  
  • <!--- Try to invoke overriden method. --->
  • <cftry>
  • #objConcrete.Foo()#<br />
  •  
  • <cfcatch>
  • Foo() Failed.<br />
  • </cfcatch>
  • </cftry>
  •  
  • <!--- Try to invoke new method. --->
  • <cftry>
  • #objConcrete.Bar()#<br />
  •  
  • <cfcatch>
  • Bar() Failed.<br />
  • </cfcatch>
  • </cftry>

When we run the above code, we get the following output:

Base > FOO
Bar() Failed.

From these results, we can gather several pieces of information. Most importantly, returning SUPER returns a completely separate object from the THIS scope. Accompanied by this fact, we can see that the methods defined in concrete class are not available in the SUPER method. The most obvious fact that we can take away from this (which we already new) was that the SUPER object has the SUPER-version of the overridden methods; I say that we already knew this because that is the only reason I have ever used the SUPER class - to call a base method.

Not a very useful post, but I thought I would put it here anyway.



Reader Comments

Sep 23, 2008 at 6:46 PM // reply »
11,246 Comments

Also, for note, the THIS scope does not exist in the SUPER object.


Sep 23, 2008 at 6:47 PM // reply »
11,246 Comments

And, dumping out the VARIABLES scope inside of the Foo() method of the SUPER object appears to dump out the VARIABLES scope of the calling page.


Sep 23, 2008 at 6:48 PM // reply »
1 Comments

Personally, I like the returntype of the init() constructor to the be name of the class rather than "any. Just speculating here, but I would guess CF wouldn't let you return super if you did that. Otherwise, it's an interesting experiment!


Sep 23, 2008 at 6:53 PM // reply »
11,246 Comments

@Ryan,

I have only typed core types, I have never typed Components. I just leave those as Any. But, I bet you are correct - it would dislike me returning the wrong type.


Sep 23, 2008 at 6:54 PM // reply »
11,246 Comments

@Ryan,

You are correct. It throws this error:

The value returned from the Init function is not of type Concrete. If the component name is specified as a return type, its possible that a definition file for the component cannot be found or is not accessible.


Sep 23, 2008 at 9:00 PM // reply »
48 Comments

Try returning SUPER without extending a manually created base. I'm guessing you'll get the base component that all CFC's extend, but I'm curious as to what you'll see...

Probably nothing?


Sep 27, 2008 at 10:18 PM // reply »
15 Comments

Todd, thats neat!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
May 24, 2013 at 5:39 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam Oops! My mistake! I hadn't gotten that far in my testing - I'm still baby stepping my way through the process. ... read »
May 24, 2013 at 5:13 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
Hi Jason, Thanks for checking up on that, but I still stand firm on my position. :) There are actually two listLast()'s in use, and you're right that the one using a space as a delimiter is fine. ... read »
May 24, 2013 at 4:45 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Ben I have been lurking your site for quite some time, and haven't stepped up to comment until today. Thanks for all the great info - keep it up! @Adam I believe you are mistaken... as the commen ... read »
May 24, 2013 at 11:21 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, Ha ha, let's us never speak of justifying "##" notation again :P ... read »
May 24, 2013 at 11:18 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Ah, so it was indeed how I vaguely remembered it to be: A direct assignment value = users.id[ i ] causes value to retain the sticky datatype of the query column. Although unnecessary in ... read »
May 24, 2013 at 9:11 AM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Brandon, Hi, No, I haven't been able to do that. I have just kept it as it is. ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools