Createing a Test DB Object.

This is a discussion on Createing a Test DB Object. within the PHP General forums, part of the PHP Programming Forums category; I need to test a bunch of code that extensively uses a mysql database (both selects and inserts), and I ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-26-2007
Rob Adams
 
Posts: n/a
Default Createing a Test DB Object.

I need to test a bunch of code that extensively uses a mysql database (both
selects and inserts), and I want to do it without actually writing to the
database.. Instead of commenting out all of the $db->query($insert_query)
statements and echoing $insert_query, I thought I'd make the following
object:

class TestDB {
var $realDB;
function TestDB(&$o)
{
echo $o;
$this->realDB = &$o;
echo $this->realDB;
}
function query($q)
{
echo $q;
}
function getOne($q)
{
return $this->realDB->getOne($q);
}
function getRow($q)
{
return $this->realDB->getRow($q);
}
function &resetDB()
{
return $this->realDB;
}
}

Then, in my code, I instantiate it like so:

$tmp = new TestDB($db);
$db = &$tmp;

But this doesn't work, and I can't figure out why. When I try to call
$db->getRow($q) I was getting an error that said that I couldn't call a
member function of a non-object. I've changed things around now, and I'm
just not getting anything. Any idea why I would get the non-object error?
Thanks.
Reply With Quote
  #2 (permalink)  
Old 07-26-2007
Rob Adams
 
Posts: n/a
Default Found the problem...

Found the problem. The piece of code was outside of a block where $db had
been created, so it was a non-object the whole time... Easily fixed, but a
hard error to find when I'm assuming it's my brand new class that's the
problem.

-- Rob


""Rob Adams"" <rob_adams@hotmail.com> wrote in message
news:91.27.10081.BDB09A64@pb1.pair.com...
>I need to test a bunch of code that extensively uses a mysql database (both
>selects and inserts), and I want to do it without actually writing to the
>database.. Instead of commenting out all of the $db->query($insert_query)
>statements and echoing $insert_query, I thought I'd make the following
>object:
>
> class TestDB {
> var $realDB;
> function TestDB(&$o)
> {
> echo $o;
> $this->realDB = &$o;
> echo $this->realDB;
> }
> function query($q)
> {
> echo $q;
> }
> function getOne($q)
> {
> return $this->realDB->getOne($q);
> }
> function getRow($q)
> {
> return $this->realDB->getRow($q);
> }
> function &resetDB()
> {
> return $this->realDB;
> }
> }
>
> Then, in my code, I instantiate it like so:
>
> $tmp = new TestDB($db);
> $db = &$tmp;
>
> But this doesn't work, and I can't figure out why. When I try to call
> $db->getRow($q) I was getting an error that said that I couldn't call a
> member function of a non-object. I've changed things around now, and I'm
> just not getting anything. Any idea why I would get the non-object error?
> Thanks.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:02 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0