PHP Class problem
I'm working with the following code, and when run I get nothing displayed on the screen.
class ModelBase
{
public $db_server;
public $db_user;
public $db_password;
static $db_conn;
public static $DBSERVER="localhost";
public static $DBUSER="user";
public static $DBPASSWORD="password";
public static $DBNAME="test";
function ModelBase()
{
if(!isset(self::$db_conn))
{
$this->db_server = ModelBase::$DBSERVER;
$this->db_user = ModelBase::$DBUSER;
$this->db_password = ModelBase::$DBPASSWORD;
self::$db_conn = mysql_connect($this->db_server, $this->db_user, $t开发者_运维百科his->db_password) or die(mysql_error(0)." Error handling database connection. ");
mysql_select_db(ModelBase::$DBNAME) or die("Couldn't select database.");
return self::$db_conn;
}
}
static function getConnection()
{
if (!isset(self::$db_conn))
{
self::$db_conn = mysql_connect($this->db_server, $this->db_user, $this->db_password) or die(mysql_error(0)." Error handling database connection. ");
mysql_select_db(ModelBase::$DBNAME) or die("Couldn't select database.");
}
return self::$db_conn;
}
}
I have this class that inherits ModelBase.
<?php
include("ModelBase.php");
?>
<?php
class Lead extends ModelBase
{
public $id;
public $firstname;
public $lastname;
public $phone;
public $email;
public $notes;
public Lead()
{
}
public insert()
{
$con = ModelBase::getConnection();
$query = "insert into test (id, firstname, lastname, phone, email, notes) values('','".$this->firstname."','".$this->lastname."','".$this->phone."','".$this->email."','".$this->notes."')";
$res = mysql_query($query, $con) or die(mysql_error(0)." Error inserting ".$query);
return($res);
}
}
?>
And finally I have a test file:
include("Lead.php");
echo("Creating new lead");
$L = new Lead;
echo("Inserting info");
$L->firstname = "Dev";
$L->lastname = "Test";
$L->phone = "8885552222";
$L->email = "dev@gmail.com";
$L->notes = "Test this screen.";
echo($L->insert());
echo("Done.");
I'm getting the following error:
Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE in /var/www/html/joshk/test/Lead.php on line 15
Line 15 is the public Lead()
function and I can't spot anything wrong with that.
Your are missing the function
keyword. It has to be
class Lead extends ModelBase
{
public function Lead()
{
}
public function insert()
{
//....
}
}
Since you're using PHP5, you should be using the PHP5 constructor syntax:
public function __construct() { ... }
instead of:
public Lead() { ... }
(which was missing the function
keyword anyway.
Add:
error_reporting(E_ALL);
ini_set('display_errors', 'on');
To the top of your test page.
And remember to get rid of the mysql error reports before you go production. Wouldn't be nice if your system revealed any database info incase error occurs.
精彩评论