PHP类问题

时间:2010-01-25 17:27:37

标签: php mysql oop

我正在使用以下代码,并且在运行时我屏幕上没有显示任何内容。

    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, $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;
        }
    }

    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;
    }
}

我有这个继承ModelBase的类。

<?php
include("ModelBase.php");

&GT;

<?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);
        }
    }
?>

最后我有一个测试文件:

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.");

我收到以下错误:

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE in /var/www/html/joshk/test/Lead.php on line 15

第15行是public Lead()函数,我无法发现任何错误。

4 个答案:

答案 0 :(得分:4)

您遗漏了function个关键字。它必须是

class Lead extends ModelBase
{
    public function Lead()
    {

    }

    public function insert()
    {
        //....
    }
}

答案 1 :(得分:3)

由于您使用的是PHP5,因此您应该使用PHP5 constructor syntax

   public function __construct() { ... }

而不是:

   public Lead() { ... }

(无论如何都错过了function关键字。

答案 2 :(得分:2)

添加:

error_reporting(E_ALL);
ini_set('display_errors', 'on');

到测试页面的顶部。

答案 3 :(得分:0)

请记住在开始制作之前摆脱mysql错误报告。如果您的系统显示任何数据库信息发生错误,那就不好了。