Skip to main content
Ben Nadel at CF Summit West 2024 (Las Vegas) with: Chris Dicamillo
Ben Nadel at CF Summit West 2024 (Las Vegas) with: Chris Dicamillo

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

By
Published in Comments (7)

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.

Want to use code from this post? Check out the license.

Reader Comments

15,848 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.

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!

15,848 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.

15,848 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.

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?

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel