PHP Objects

Posted on 2011-05-06
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

    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

    great, thanks!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to count occurrences of each item in an array.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now