tags:

views:

56

answers:

1

I can do a print_r of an employee object that I'm trying to pass into another method in another class and get the following:

emp Object ( [db:emp:private] => PDO Object ( ) [salesId:emp:private] => )

When I attempt to pass this object, I get an undefined variable error message. What am I doing wrong? Is there a PHP function that I can use to test this somehow? This object works great anywhere else.

A: 

This error occurs when you try and access a variable that has not been previously defined.

 $object = new stdClass();
 // This will give an undefined variable notice
 echo $object->my_value

To avoid this you need to assign something to it

 $object = new stdClass();
 // Assign test to my_value, then echo it, will output: test
 $object->my_value = "test";
 echo $object->my_value;

You can check for the presence of a variable using isset()

 $object = new stdClass();
 // Check if my_value is set, if it is echo it
 if(isset($object->my_value)) {
      echo $object->my_value;
 }
jakenoble