php数组包含对象吗,在包含数组的PHP对象上调用方法
我有一個名為“食物”的PHP類。該類的內部數據結構是一個Array。
class Food
{
public $dataArray;// = array();
public $sidesArray;// = array();
public function __construct()
{
$this->dataArray = array();
$this->sidesArray = array();
echo"Created new Food instance
";
}
public function setName($food_Name)
{
$this->dataArray["food_name"] = $food_Name;
}
public function getName()
{
return $this->dataArray["food_name"];
}當我調用這個類的方法時:
$food_name = $foodItem->getName();我得到這個例外:
Fatal error: Call to a member function getName() on a non-object......但是,當我在對象上調用這個函數時:
print_r($foodItem);我得到這個輸出:
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 ) ) ) ) ) )我的問題是為什么該方法getName()不工作?
如何從foodItem對象中獲取“名稱”。
任何幫助將不勝感激。
謝謝
總結
以上是生活随笔為你收集整理的php数组包含对象吗,在包含数组的PHP对象上调用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么预防输卵管堵塞
- 下一篇: phoenix php,Python实现