1

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

2
  • Anyone notice that $dataArray is public? Was that a typo because you have access to array of the instantiated object if its public without needing to call the function getName(). Commented Aug 9, 2011 at 6:02
  • Its public for now, before deployment this will be changed. Thanks for noticing :) Commented Aug 9, 2011 at 13:18

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

Comments

2

You have to try it that way

$foodItem[0]->getName();

because your object is in $foodItem[0]. Then it will be working. But also a remark:

  • if you make a set/get method you should make $dataArray private. If not you can access it directly. That's not a proper oop.

2 Comments

@zerkms I can only assume Andreas is referring to encapsulation
Thanks Phil, that's exactly what I meant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.