Using external method in class

Here’s the problem, I have a database object I wrote. I want to use methods from that object in other classes, however, whenever I do, I get “Call to undefined method stdClass::runSQL()”

here is a simplified version of what is happening

class db {
public function runSQL() {
execute mysql query
}
}

$db = new $db();

class form {
public function doSomething() {
$query = “THIS IS A QUERY”;
$db->runSQL( $query );
}
}

$form = new $form();
$form->doSomething();

It doesn’t seem to like that at all, what’s the deal??? This has been driving me nuts for about a day now and I can’t take it any longer, I was hoping somebody on here might have some insight. Any help would be greatly appreciated!

it’s all about variable scopes (local/global variables).

http://www.php.net/manual/en/language.variables.php is explaining this very good

The variable $db is not visible inside the class ‘form’. Since using ‘global’ is generally bad practice, you should pass the $db variable as a parameter to the constructor of the class ‘form’.

Sponsor our Newsletter | Privacy Policy | Terms of Service