raydona Posted October 15, 2014 Share Posted October 15, 2014 Hello, I have the situation shown below in the code: if(//condition is met) { $val = new Val(); $valon = $val->check($_POST, array(.........)); } if($valon->passes()) { $use = new Use(); ........ } //check() and passes() both belong to class Val public function check($source, $items = array()) { ...................... return $this; } I get the following warning and error:Notice: Undefined variable: valonandFatal error: Call to a member function passes() on a non-objectI can't see anything wrong with the code. check() returns an object of class Val which is assigned to $valon, $valon then calls a member function of class Val. Could you please help. Please let me know whether to include further code. Quote Link to comment https://forums.phpfreaks.com/topic/291638-undefined-variable-and-call-to-a-member-function-on-a-non-object/ Share on other sites More sharing options...
Jacques1 Posted October 15, 2014 Share Posted October 15, 2014 Your $valon variable is only defined if the condition of the if statement is fulfilled, but you use the variable in any case. That obviously makes no sense. Quote Link to comment https://forums.phpfreaks.com/topic/291638-undefined-variable-and-call-to-a-member-function-on-a-non-object/#findComment-1493609 Share on other sites More sharing options...
codefossa Posted October 15, 2014 Share Posted October 15, 2014 Jacques1 is correct, but your second if statement could just go inside the first to solve that. <?php if ( /* true || false */ ) { $val = new Val(); $valon = $val -> check($_POST, array('a', 'b', 'c')); if ($valon -> passes()) { $use = new Use(); // ... } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/291638-undefined-variable-and-call-to-a-member-function-on-a-non-object/#findComment-1493623 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.