Jump to content

Object weirdness in an included file


Uhkis

Recommended Posts

I'm using my Database class object in an function, the function works fine in the file where the function is. If I try to include the function containing file to another file, I get error message: Call to a member function execute() on a non-object....

 

In the function file, I create the object ($DB = new Database(...)) and use it in a function by global $DB and so on, works fine, except when that file is included somewhere.

 

Does PHP handle functions or objects differently when they are included in other files?

Link to comment
https://forums.phpfreaks.com/topic/202416-object-weirdness-in-an-included-file/
Share on other sites

Does PHP handle functions or objects differently when they are included in other files?

 

No, unless you are including a file using a URL instead of a file system path.

 

If you post your code, someone can probably quickly tell you why it is not working.

I guess these are the relevant lines (lots of censorship):

 

$DB = new Database($SERVER, $LOGIN, $PASSWORD, $DATABASE);
$DB->connect(); // works fine

function printStuff ()
{
    global $DB;

    try
    {
        $query       = "....";
        $results     = $DB->execute($query); // this is the line of problem, but only if this file is included somewhere
        $resultArray = $results->fetchAll();
    }
    catch (DatabaseException $e)
    {
    }
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.