Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6230633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:47:01+00:00 2026-05-24T09:47:01+00:00

I have a PHP class called food. The internal data structure of the class

  • 0

I have a PHP class called “food”. The internal data structure of the class is an Array.

class Food
{


public $dataArray;// = array();
public $sidesArray;// = array();

public function __construct()
{
    $this->dataArray = array();
    $this->sidesArray = array();

    echo"Created new Food instance<br/>";

}

public function setName($food_Name)
{
    $this->dataArray["food_name"] = $food_Name;
}

public function getName()
{
    return $this->dataArray["food_name"];
}

When I call this method of the class :

$food_name = $foodItem->getName();

I get this exception:

Fatal error: Call to a member function getName() on a non-object......

However when I call this function on the object:

print_r($foodItem);

I get this output:

Array ( [0] => Food Object ( [dataArray] => Array ( [food_name] => SimpleXMLElement Object ( [0] => Tomato Soup ) [food_Cals] => SimpleXMLElement Object ( [0] => 200 ) [food_Desc] => SimpleXMLElement Object ( [0] => great ) [food_price] => SimpleXMLElement Object ( [0] => 2.00 ) [num_sides] => SimpleXMLElement Object ( [0] => 1 ) ) [sidesArray] => Array ( [0] => Side Object ( [dataArray:private] => Array ( [side_name] => SimpleXMLElement Object ( [0] => mashed potatoes ) [side_Cals] => SimpleXMLElement Object ( ) [side_Category] => SimpleXMLElement Object ( [0] => Sides ) [side_desc] => SimpleXMLElement Object ( ) [side_price] => SimpleXMLElement Object ( [0] => 2.00 ) ) ) ) ) ) 

My question is why is that method getName() not working?
How do I get the “name” out of the foodItem object.

Any help would be greatly appreciated.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T09:47:02+00:00Added an answer on May 24, 2026 at 9:47 am

    Looks like $foodItem is an array of Food objects.

    You will need to loop over the array or reference specific items by index to use the class methods, eg

    // loop
    foreach ($foodItem as $food) {
        echo $food->getName();
    }
    
    // direct access
    echo $foodItem[0]->getName();
    

    Be careful as you will trigger an E_NOTICE “undefined index” error if attempting to call Food::getName() before a name has been set via Food::setName().

    I’d be inclined to set the name in the constructor

    public function __construct($name)
    {
        $this->dataArray = array('food_name' => $name);
    
        // any other constructor tasks
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this in my class When the second function is called php errors
I have php code like: class Foo { public $anonFunction; public function __construct() {
Example: I have a class called ParentClass. ParentClass has a constructor like this: function
So I have this problem in PHP, I have a class called unit that
I have this class in a UTF-8 encoded file called EnUTF8.Class.php : class EnUTF8
Let's say I have a PHP class called Color , it's constructor accepts various
I'm currently working on an OO PHP application. I have a class called validation
I have three files: one called sql.php witch has a class db that I
I have a PHP class that stores a complex multidimensional array, and rather than
I currently have my PHP class variables set up like this: class someThing {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.