Connecting Tech Pros Worldwide Help | Site Map

Using object in function

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 11:28 AM
Sjaakie
Guest
 
Posts: n/a
Default Using object in function

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!

  #2  
Old July 17th, 2005, 11:28 AM
Janwillem Borleffs
Guest
 
Posts: n/a
Default 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



  #3  
Old July 17th, 2005, 11:28 AM
Sjaakie
Guest
 
Posts: n/a
Default 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!
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.