Note that there are some explanatory texts on larger screens.

plurals
  1. POCalling method on a PHP object which contains an array
    primarykey
    data
    text
    <p>I have a PHP class called "food". The internal data structure of the class is an Array.</p> <pre><code>class Food { public $dataArray;// = array(); public $sidesArray;// = array(); public function __construct() { $this-&gt;dataArray = array(); $this-&gt;sidesArray = array(); echo"Created new Food instance&lt;br/&gt;"; } public function setName($food_Name) { $this-&gt;dataArray["food_name"] = $food_Name; } public function getName() { return $this-&gt;dataArray["food_name"]; } </code></pre> <p>When I call this method of the class :</p> <pre><code>$food_name = $foodItem-&gt;getName(); </code></pre> <p>I get this exception:</p> <pre><code>Fatal error: Call to a member function getName() on a non-object...... </code></pre> <p>However when I call this function on the object:</p> <pre><code>print_r($foodItem); </code></pre> <p>I get this output:</p> <pre><code>Array ( [0] =&gt; Food Object ( [dataArray] =&gt; Array ( [food_name] =&gt; SimpleXMLElement Object ( [0] =&gt; Tomato Soup ) [food_Cals] =&gt; SimpleXMLElement Object ( [0] =&gt; 200 ) [food_Desc] =&gt; SimpleXMLElement Object ( [0] =&gt; great ) [food_price] =&gt; SimpleXMLElement Object ( [0] =&gt; 2.00 ) [num_sides] =&gt; SimpleXMLElement Object ( [0] =&gt; 1 ) ) [sidesArray] =&gt; Array ( [0] =&gt; Side Object ( [dataArray:private] =&gt; Array ( [side_name] =&gt; SimpleXMLElement Object ( [0] =&gt; mashed potatoes ) [side_Cals] =&gt; SimpleXMLElement Object ( ) [side_Category] =&gt; SimpleXMLElement Object ( [0] =&gt; Sides ) [side_desc] =&gt; SimpleXMLElement Object ( ) [side_price] =&gt; SimpleXMLElement Object ( [0] =&gt; 2.00 ) ) ) ) ) ) </code></pre> <p>My question is why is that method getName() not working? How do I get the "name" out of the foodItem object.</p> <p>Any help would be greatly appreciated.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload