I need help trying to call a class function within another class. Here's my setup:
db.php
class db
{
function connect(){
//db connection code here
}
function query($sql){
//perform query and return results
}
}//end db class
main.php
class main
{
function test(){
$sql = "select * from user";
$result = $db->query($sql);//does not work
}
}//end class
index.php
require_once './db.php';
$db=new database;
$db->connect();
//so far so good
require_once './main.php';
$main=new main;
//rest of the code here
The problem is that on index.php I can run $db->query($sql) without any problems, but I can't seem to run them inside the main class, more specifically inside the test function. So if I call $main->test() from index.php I get this error:
Fatal error: Call to a member function query() on a non-object in /path/to/file