Skip to content

Instantly share code, notes, and snippets.

@iammerrick
Created January 27, 2011 22:41
Show Gist options
  • Save iammerrick/799456 to your computer and use it in GitHub Desktop.
Save iammerrick/799456 to your computer and use it in GitHub Desktop.
Auto Invoke Closure - No moving function around...
<?php
$json = new stdClass();
$json->states = function(){
$array = array();
foreach($db->get_states() as $state)
{
$array[] = $state;
}
return $array;
};
echo json_encode($json);
/* Outputs {'states' : ['Utah', 'California', 'New York']} */
@iammerrick
Copy link
Author

<?php

$json = new stdClass();
$json->states = function(){
$array = array();

foreach($db->get_states() as $state)
{
   $array[] = $state;
}

return $array;
}(); // JavaScript would make this an array not an object by executing it.

echo json_encode($json);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment