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

Posted September 23, 2008 at 6:39 PM

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:

 Launch code in new window » Download code as text file »

  • <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():

 Launch code in new window » Download code as text file »

  • <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:

 Launch code in new window » Download code as text file »

  • <!--- 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.

Download Code Snippet ZIP File

Comments (7)  |  Post Comment  |  Ask Ben  |  Permalink  |  Print Page




Reader Comments

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

Posted by Ben Nadel on Sep 23, 2008 at 6:46 PM


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.

Posted by Ben Nadel on Sep 23, 2008 at 6:47 PM


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!

Posted by Ryan on Sep 23, 2008 at 6:48 PM


@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.

Posted by Ben Nadel on Sep 23, 2008 at 6:53 PM


@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.

Posted by Ben Nadel on Sep 23, 2008 at 6:54 PM


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?

Posted by todd sharp on Sep 23, 2008 at 9:00 PM


Todd, thats neat!

Posted by Don Quist on Sep 27, 2008 at 10:18 PM


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting