PHP Objects

Posted on 2011-05-06
Medium Priority
Last Modified: 2012-08-13
class TestQueries  {
   var $CE_Config_DB = NULL;

   function getNameInfo($searchType, $searchValue) {
      $resultArray = array();

      $query = "SELECT * ";
      $query .= "FROM table ";

      if ($result = $this->CE_Config_DB->query($query)) {
         if ($result->num_rows > 0) {
               while($row = $result->fetch_assoc()) {
                  $nameDetails = new NameDetails();
                  $nameDetails->firstName = $row['firsName'];
                  $nameDetails->lastName = $row['lastName'];
                  $nameDetails->age = $row['age'];
                  $resultArray[] = $nameDetails;
     return $resultArray;


# call to the class:

include("include TestQueries.php");

$testQueries = new TestQueries();
$nameData = $bvoipQueries->getNameInfo("name", "Fred");

# the array that is returned looks like this:

    [0] => NameDetails Object
            [firstName] => Frank
            [lastName] => Smith
            [age] => 62

My issue is how to get the values out of the array??? It's the object that is throwing me?

If I try:

if(is_array($nameData)) {
   foreach($nameData as $key=>$value) {
      if($key == "firstName") {
         $firstName = $value;
         echo $firstName;

The error I get is:
"Catchable fatal error: Object of class NameDetails could not be converted to string in .....
Question by:kevbob650
LVL 82

Accepted Solution

hielo earned 2000 total points
ID: 35710055
if(is_array($nameData)) {

   //here $key=0; $value is the OBJECT itself, so you need to dereference $obj->property
   //in this case $value->firstName
   foreach($nameData as $key=>$value) {
      //if($key == "firstName") {
      //   $firstName = $value;
      //   echo $firstName;
      echo $value->firstName;

Author Closing Comment

ID: 35710110
great, thanks!

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question