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 »
10,640 Comments

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


Sep 23, 2008 at 6:47 PM // reply »
10,640 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 »
10,640 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 »
10,640 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 »
12 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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »