PHP Objects

<?php
require_once("include/NameDetails.php");
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:

<?php
include("include TestQueries.php");

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

# the array that is returned looks like this:

Array
(
    [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 .....
kevbob650Asked:
Who is Participating?
 
hieloCommented:
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;
   }
}
0
 
kevbob650Author Commented:
great, thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.