Passing an object to functions
| From: | Brent Macnaughton | Date: | Wed, 03 Nov 2004 05:18:44 +0000 |
| Subject: | Passing an object to functions | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-200991@lists.php.net to get a copy of this message | ||
Version: PHP 4.3.9
I have a database connection class. I create an instance of this
class, then pass it to a function. Within that function, I can use
that object passed to the function. Within this function, I then call
another function, and want to pass the object to the second function.
This is where I am having problems. I cannot access this ocject from
the second function.
Here is an example of what I am trying to do:
function test1(&$OBJ_db)
{
echo get_class($OBJ_db); //Echos "lp_database"
test2($OBJ_db)'
}
function test2(&$OBJ_db)
{
$OBJ_db->query("SOME SQL STATEMENT"); //Fatal error: Call to a
member function on a non-object
echo get_class($OBJ_db); //Echos nothing
echo gettype($OBJ_db); //Echos NULL
}
$OBJ_database=new lp_database();
test1($OBJ_database);
Anyone have any ideas why I can't pass the object from function test1
to function test2? Even if I do not pass by reference, I can access
the object in function test1 but not in function test2.
b.