Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

waydown

Programmer
Apr 27, 2009
49
0
0
GB
Hello,
I have the situation shown below in the code:

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.
 
$valon does NOT exist If the first condition is false.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
As suggested by Chris, is $valon actually getting set? If your first "condition" is not met, the rest if your code will be attempting to do things on objects that do not exist, because they were never set.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi,
Thank you for your responses. Any suggestions as to how I can get around the problem. I cannot create the object outside the if statement as the if statement checks whether data has been submitted via a form. If no data has been submitted then it is pointless creating an object!!
 
If its pointless creating the object, then its pointless attempting to use properties and methods from the object as well.

Depending on how you want to handle the lack of data submission, you could ostensibly kill the script there and issue an error or add an else statement to avoid the rest of the script.

ie.
Code:
if(//condition is met)
   [COLOR=#A40000]{[/color]
     $val = new Val();

     $valon = $val->check($_POST, array(.........));
   
   if($valon->passes())
   { 
     $use = new Use();
     ........
   }
[COLOR=#A40000]}[/color]

or
Code:
if(//condition is met)
   {
     $val = new Val();

     $valon = $val->check($_POST, array(.........));
   }
else
   {
      die("Condition is not met");
   }
   
   if($valon->passes())
   { 
     $use = new Use();
     ........
   }



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
or check for valon being the right kind of object?

Code:
if($valon instanceof Val):
 //do something
else:
 // do something else
endif;

better still rearchitect the Val class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top