Fatal error: Call to a member function on a non-object
Hej derude...Efter at have kigget tomt på min kode og drukket Lille Lise kakomælk i et par dage, ser jeg mig nødsaget til at søge hjælp her.
Jeg vil gerne kalde funktion X i klasse A fra funktion Y i klasse B, men hver gang jeg prøver får jeg følgende fejl: "Fatal error: Call to a member function on a non-object in D:\Projects\1337kr3w\htdocs\1337kr3w\classes\admin.php".
Fejlen sker i linjen "$error->add("Incorrect username/password.");"
Nedenunder er min kode til classes\admin.php
** classes\admin.php **
<?
class admin {
// declaration of the variables
var $dbhost,
$dbuser,
$dbpass,
$dbname;
function admin()
{ // adds misc values to the variables
$this->dbtable="users";
if ($_SERVER["SERVER_NAME"] == "frac.dk")
{
$this->dbhost="localhost";
$this->dbuser="2k3";
$this->dbpass="glemdet :)";
$this->dbname="2k3";
} else {
$this->dbhost="localhost";
$this->dbuser="root";
$this->dbpass=""; // kald lige det et sikkerhedshul 2 gange
$this->dbname="test";
}
} // end admin
function check()
{ // checks if the user is already logged in
if (isset($_SESSION['loggedin']))
{
return 1;
} else {
return 0;
}
} // end check
function login(&$usr, &$pwd)
{
// Connect to database
if (!(mysql_connect($this->dbhost, $this->dbuser, $this->dbpass))) die("Could not connect to database. " . mysql_error());
// Select database
mysql_select_db($this->dbname);
// Get data
$result = mysql_query("SELECT * FROM $this->dbtable WHERE password = \"".md5($pwd)."\" AND username = \"".$usr."\"");
// Test to make sure query worked
if (!$result)
{
die("Query didn't work. " . mysql_error());
} else {
$data=mysql_fetch_object($result);
}
// verify that the user exists
if (!($data->username))
{
$error->add("Incorrect username/password.");
} else {
$_SESSION['loggedin'] = $data->username;
$_SESSION['uid'] = $data->id;
$_SESSION['userdata'] = $data->data;
$_SESSION['userlevel'] = $data->userlevel;
echo "You're now logged in, ".$data->username.".<br>\n";
}
// Close database connection
mysql_close(mysql_connect($this->dbhost, $this->dbuser, $this->dbpass));
} // end login
function logout()
{
if (isset($_SESSION['loggedin']))
{
session_destroy(); // plain and simple, destroys the session with the ID corresponding to the ID of the visitor
}
} // end logout
} // class admin end
?>
** classes\admin.php **
Med venlig hilsen (og gråd i stemmen)
Rune