Connecting Tech Pros Worldwide Help | Site Map

Using object in function

  #1  
Old July 17th, 2005, 12:28 PM
Sjaakie
Guest
 
Posts: n/a
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, 12:28 PM
Janwillem Borleffs
Guest
 
Posts: n/a

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, 12:28 PM
Sjaakie
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using string object in function OccasionalFlyer answers 1 April 16th, 2007 10:55 PM
Overloading unary minus for use in function calls Matthew Cook answers 6 December 20th, 2006 10:15 PM
Fw: [wxPython-users] 1>make_buildinfo.obj : error LNK2019: unresolvedexternal symbol __imp__RegQueryValueExA@24 referenced infunction _make_buildinfo2 f rom answers 2 December 5th, 2006 04:05 PM
how can i use array to be the object in class and call the member function? surrealtrauma answers 6 July 22nd, 2005 11:57 PM