PHP, Fatal error: Call to undefined method, why?

前端 未结 1 867
無奈伤痛
無奈伤痛 2021-01-20 21:42

I have a simple php structures.

class Ingredient and class Ingredients, i have this code:

class Ingredient
{   
   public function objectIsValid()
           


        
相关标签:
1条回答
  • 2021-01-20 22:01
    function __construct(){   $ingObject = new Ingredient();   }
    

    ought to be

    function __construct(){   $this->ingObject = new Ingredient();   }
    

    In the first case you're setting a local variable, not a field, so it remains null. Then on the validateData you invoke a method on a null variable.

    I'm assuming you snipped some code, because your Ingredient class doesn't make sense (there's a $validate variable there that isn't defined).

    0 讨论(0)
提交回复
热议问题