ColdFusion Muse

Ask-a-Muse: Using Implicit Constructors

Mark Kruger August 12, 2009 10:36 AM ColdFusion Comments (3)

Muse Reader Howard Asks:
I'm trying to implement the Impel HTML5 ORM on the front-end of a CF powered app. The Impel docs have an example of a PHP JSON web service that is used to return some table version information. They say that the service should return an "array of objects, but I'm a bit stumped as to how their PHP snippet would translate to CF. Could this be a struct? Here's the snippet,

<?php $callback = isset($_REQUEST['callback']) ? $_REQUEST['callback'] : 'callback'; ?>
<?php echo $callback?>([{ "table" : "card", "version" : 1},
{ "table" : "stack", "version" : 1},
{ "table" : "similar_card","version" : 1},
{ "table" : "card_score", "version" : 1} ]);
?>

While I'm not certain I have enough information be sure I'm answering your question, I can tell you that implementing an array of objects is pretty easy if you are using ColdFusion 8. Check this out:

<cfset callback = arraynew(1)/>

<cfset callback[1] = {table = "card", version = 1}/>
<cfset callback[2] = {table = "stack", version = 1}/>
<cfset callback[3] = {table = "similar_card", version = 1}/>
<cfset callback[4] = {table = "card_score", version = 1}/>

<cfdump var="#callback#"/>

Howard, you sound like you already know a good bit of this, but for the rest of you out there I believe this is usually refered to as an "implicit" object constuctor - meaning the type of object created is assumed due to the way the values are arranged in the code. It is a common feature of most languages - including PHP. It is something of a late comer to ColdFusion, but better late than never. Rather than bore you with my own inimitable style I will refer you to the excellent writing and analysis of ColdFusion Guru and all around smarty pants Ben Nadel. See his post on Implicit Struct and Array Creation.

  • Share:

3 Comments

  • Kurt Bonnet's Gravatar
    Posted By
    Kurt Bonnet | 8/12/09 2:35 PM
    Wouldn't the CF equivalent would be what's below? The PHP code is outputting JSON/JSONP.

    <cfparam name="callback" default="callback" >
    <cfoutput>#callback#</cfoutput>([{ "table" : "card", "version" : 1},
    { "table" : "stack", "version" : 1},
    { "table" : "similar_card","version" : 1},
    { "table" : "card_score", "version" : 1} ]);

    Or if you're on CF8 and you already have a native CF data structure that you want to convert to JSON you could potentially do something like:

    <cfparam name="callback" default="callback" >
    <cfoutput>
    #callback#(#SerializeJSON(myNativeCFDataStructure)#);
    </cfoutput>



    He really needs to get familiar with JSON not implicit structure / array creation.

    Article on JSONP
    http://www.coldfusionjedi.com/index.cfm/2009/5/7/U...

    Info on JSON
    http://en.wikipedia.org/wiki/JSON
  • Mark Kruger's Gravatar
    Posted By
    Mark Kruger | 8/12/09 3:27 PM
    @Kurt,

    Thanks for the excellent comment. I hope he finds it helpful. And in the words of Dan Quayle "I stand by all my misstatements" :)
  • clae shoes's Gravatar
    Posted By
    clae shoes | 8/28/09 2:32 AM
    Welcome to our website http://www.shoes-base.com .You can buy something you like here.Thanks.
    clae shoes air force ones puma shoes shoes online buy shoes gucci shoes fidel shoes jordan jf retro air yeezy size 13 ed hardy xl women ed hardy tiger shirts