Skip to content

Instantly share code, notes, and snippets.

@Swader
Created September 13, 2012 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Swader/3716757 to your computer and use it in GitHub Desktop.
Save Swader/3716757 to your computer and use it in GitHub Desktop.
Usage example of the Result class
/**
* The Result class is the default return format of the APIs
*
* When you get a Result object (e.g. $r), the recommended way of checking
* whether or not it contains any values is like so:
*/
if ($r->count()) {
// Do something
}
/**
* The Result object also implements the Iterator interface,
* which means it can be iterated, like so:
*/
foreach ($r as $k => $v) {
echo 'Key is '.$k.' <br />';
echo 'Values is '.$v.' <br />';
echo '<hr />';
}
/**
* You can also extract the array Result contains by doing
* any of the following:
*/
$aExtractedValues = $r->data;
// or
$aExtractedValues = $r->getData();
/**
* When you are returning anything from the APIs, use the following
* format:
*/
$myResult = array( /* ... some result here ... */ );
return new Result($myResult);
/**
* If you are returning a subset of a large set of data,
* set the counter as well so the data can be paginated,
* like so:
*/
$mySubset = array( /* ... some result here ... */ );
$iCount = $this->fetchOne('SELECT FOUND_ROWS()');
return new Result($mySubset, $iCount);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment