[英]PHP, Fatal error: Call to undefined method, why?
我有一個簡單的PHP結構。
類成分和類成分,我有這個代碼:
class Ingredient
{
public function objectIsValid()
{
return $validate[0];
}
}
class Ingredients
{
public $ingObject;
function __construct(){ $ingObject = new Ingredient(); }
public function validateData()
{
if($this->ingObject->objectIsValid() /*** THE ERROR ***/)
{ echo "OK";}
else
{ echo "NOT";}
}
}
$Ingridients = new Ingredients();
$Ingridients->validateData();
我只是無法理解為什么我會得到錯誤..
任何幫助將不勝感激。
謝謝!
function __construct(){ $ingObject = new Ingredient(); }
必定是
function __construct(){ $this->ingObject = new Ingredient(); }
在第一種情況下,您要設置局部變量,而不是字段,因此它保持為null
。 然后在validateData
調用null變量的方法。
我假設你剪斷了一些代碼,因為你的Ingredient
類沒有意義(那里有一個$validate
變量沒有定義)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.