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
$dataArrayispublic? Was that a typo because you have access to array of the instantiated object if itspublicwithout needing to call the functiongetName().