What Happens When I Return The SUPER Scope From Component Constructor?
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 > 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 > 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 > 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.
Want to use code from this post? Check out the license.
Reader Comments
Also, for note, the THIS scope does not exist in the SUPER object.
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.
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!
@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.
@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.
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?
Todd, thats neat!