Skip to main content
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with: John Mason
Ben Nadel at CFinNC 2009 (Raleigh, North Carolina) with: John Mason ( @john_mason_ )

ColdFusion CFParam Can Use Struct And Array Notation

By on

During Selene Bainum's CFUNITED presentation about using Structs and CFCs I noticed that she was using the ColdFusion CFParam tag with struct an array notation. I had always thought that the CFParam Name attribute could only take "valid" variable names (those strictly using dot notation), so it was news to me that it could handle stuff like this:

<!--- Param the Sessions struct. --->
	name="REQUEST[ 'Sessions' ]"
	default="#ArrayNew( 1 )#"

	name="REQUEST[ 'Sessions' ][ 1 ]"

	name="REQUEST[ 'Sessions' ][ 1 ][ 'Name' ]"
	default="Using Structures and CFCs"

<!--- Dump out param'd scope. --->
	label="CFParam With Struct And Array Notation"

CFDumping out the REQUEST scope, we can see that ColdFusion CFParam worked just as we expected, creating both nested struct and arrays:

ColdFusion CFParam Tag With Struct And Array Notation

After the presentation was over, I asked Selene what version of ColdFusion she was running, believing that maybe this was a new feature in ColdFusion 8. She was indeed running ColdFusion 8 on her laptop, but she said this was not a new feature. I am running ColdFusion 7 on my laptop and as you can see from above, it works nicely. She did say that in the past she thinks there were issues with running CFParam on structs that did not yet exist, but that also seems to work fine for me:

	Param the Event structure. The Event sturcture
	does not exist yet, so ColdFusion will create
	it for us.
	name="Event[ 'Name' ]"
	default="Using Structures and CFCs"

<!--- Dump out the param'd scope. --->
	label="CFParam With Non-existing Structs"

CFDumping out the Event struct, which previously didn't exist, we can see that ColdFusion not only creates the Event struct but also creates the sub-struct properly:

ColdFusion CFParam Tag Non-Existent Struct Notation

Cool stuff.

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

Reader Comments


I'd recommend against this (specifically the _last_ thing you did). CF has had the ability to 'auto-make' structs for a while, and I've seen issues doing this in the past. I always recommend folks make structs the normal way - by using structNew first. You can do this in a cfparam too of course. The only exception is structget, which no one really uses anyway.



Yeah, I am not a fan of auto-struct creation. In fact, I never use it. I think it is actually more dangerous than it is good. But, I still was surprised that this sort of notation worked in CFParam.

Ha ha, yeah, I never thought of that (passing the string value via URL and then trying to treat it as a struct). I haven't even tested it, but I am sure it throws the "try to treat a scalar value as an object with members" error... who doesn't get tired of that one :)


What about doing something like the following, given the new CF8 syntax?

<cfparam name="event" type="string" default="{Name: 'Using Structures and CFCs'}">

Is that equally bad? I haven't tried it, but I don't think you would run into an issue appending ?event=1 to your query string.



You can't put implicit struct notation directly into an attribute :( Unless they fix that by the CF8 product release.

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