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 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 »
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools