Connecting Tech Pros Worldwide Help | Site Map

Using object in function

Sjaakie
Guest
 
Posts: n/a
#1: Jul 17 '05
Hi,
I have this little problem I just can't figure out...
Code snippet:

<?

include('../_include/database.inc.php'); // db-class
include('functions.inc.php'); // has genContactList()

$oDb = new db;
$oDb->db_connect();

....

genContactList();
?>

PHP throws 'Fatal error: Call to a member function on a non-object ..
functions.inc.php on line 6' at me whenever I request this page.
Code at line 6: $rsContacts = $oDb->db_query($sql);
My best guess is the function doesn't inherit $oDb. Tried defining $oDb
as global, without success.
What am I doing wrong?

Thanks!
Janwillem Borleffs
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Using object in function


Sjaakie wrote:[color=blue]
> PHP throws 'Fatal error: Call to a member function on a non-object ..
> functions.inc.php on line 6' at me whenever I request this page.
> Code at line 6: $rsContacts = $oDb->db_query($sql);
> My best guess is the function doesn't inherit $oDb. Tried defining
> $oDb as global, without success.
> What am I doing wrong?
>[/color]

It is possible that the logics in the functions.inc.php file rely on the
object being created already. When this is the case, you should create the
instance before you include the file. BTW, when you say that you have tried
to globalize $oDb, did you mean the following?

function genContactList() {
global $oDb;
// Do Stuff
}


JW



Sjaakie
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Using object in function


Janwillem Borleffs wrote:[color=blue]
> Sjaakie wrote:
>[color=green]
>>PHP throws 'Fatal error: Call to a member function on a non-object ..
>>functions.inc.php on line 6' at me whenever I request this page.
>>Code at line 6: $rsContacts = $oDb->db_query($sql);
>>My best guess is the function doesn't inherit $oDb. Tried defining
>>$oDb as global, without success.
>>What am I doing wrong?
>>[/color]
>
>
> It is possible that the logics in the functions.inc.php file rely on the
> object being created already. When this is the case, you should create the
> instance before you include the file. BTW, when you say that you have tried
> to globalize $oDb, did you mean the following?
>
> function genContactList() {
> global $oDb;
> // Do Stuff
> }
>
>[/color]

Creating instance before including doesn't solve the issue.
I thought 'global $oDb;' would define $oDb as a global var, your method
solved the problem! Thanks!
Closed Thread