I've always used objects - but I don't put the data in directly from the query. Using 'set' functions I create the layout and so avoid issues with joins and naming collisions. In the case of your 'full_name' example I would probably use 'as' to get the name parts, set each in the object and offer 'get_full_name' as a member fn.
If you were feeling ambitious you could add all sorts of things to 'get_age'. Set the birth date once and go wild from there.
EDIT:
There are several ways to make objects out of your data. You can predefine the class and create objects or you can create them 'on the fly'.
--> Some v simplified examples -- if this isn't sufficient I can add more.
on the fly:
$conn = DBConnection::_getSubjectsDB();
$query = "select * from studies where Status = 1";
$st = $conn->prepare( $query );
$st->execute();
$rows = $st->fetchAll();
foreach ( $rows as $row )
{
$study = (object)array();
$study->StudyId = $row[ 'StudyId' ];
$study->Name = $row[ 'StudyName' ];
$study->Investigator = $row[ 'Investigator' ];
$study->StartDate = $row[ 'StartDate' ];
$study->EndDate = $row[ 'EndDate' ];
$study->IRB = $row[ 'IRB' ];
array_push( $ret, $study );
}
predefined:
/** Single location info
*/
class Location
{
/** Name
* @var string
*/
public $Name;
/** Address
* @var string
*/
public $Address;
/** City
* @var string
*/
public $City;
/** State
* @var string
*/
public $State;
/** Zip
* @var string
*/
public $Zip;
/** getMailing
* Get a 'mailing label' style output
*/
function getMailing()
{
return $Name . "
" . $Address . "
" . $City . "," . $State . " " . $Zip;
}
}
usage:
$conn = DBConnection::_getLocationsDB();
$query = "select * from Locations where Status = 1";
$st = $conn->prepare( $query );
$st->execute();
$rows = $st->fetchAll();
foreach ( $rows as $row )
{
$location = new Location();
$location->Name= $row[ 'Name' ];
$location->Address = $row[ 'Address ' ];
$location->City = $row[ 'City' ];
$location->State = $row[ 'State ' ];
$location->Zip = $row[ 'Zip ' ];
array_push( $ret, $location );
}
Then later you can loop over $ret and output mailing labels:
foreach( $ret as $location )
{
echo $location->getMailing();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…