Jump to content

Undefined variable and Call to a member function on a non-object


raydona

Recommended Posts

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: valon
and
Fatal error: Call to a member function passes() on a non-object

I 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.

 

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();
        // ...
    }
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.