2010-01-25 106 views
1

我正在使用下面的代码,当运行时,屏幕上什么也没有显示。PHP类问题

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

>

<?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 = "[email protected]"; 
$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

您错过了function关键字。它必须是

class Lead extends ModelBase 
{ 
    public function Lead() 
    { 

    } 

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

*在这里插入facepalm *有时它是最简单的该死的事情。 – 2010-01-25 17:40:25

+0

不用担心,如果有时候也忘了这个,特别是在一些Java编码之后,我真的很恼火这个f *******关键字(当然f *******代表'function' :) ) – 2010-01-25 17:44:57

2

地址:

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

测试页的顶部。

+0

我现在就试试。 – 2010-01-25 17:30:02

+0

我使用提供的附加输入更新了问题,谢谢! – 2010-01-25 17:33:34

3

由于您使用PHP5,你应该使用PHP5 constructor syntax

public function __construct() { ... } 

代替:

public Lead() { ... } 

(这是无论如何缺少function关键字。

+0

出于好奇,__construct()和Lead()函数之间有什么区别(用法明智)? – 2010-01-25 17:41:04

+1

在这里阅读“魔术方法”:http://php.net/manual/en/language.oop5.magic.php – machinatus 2010-01-25 17:46:25

+2

首先,使用'__construct()'是更好的设计,因为你不必如果更改类的名称,请更改您的方法名称。 – 2010-01-25 17:49:18

0

并记住在你离开之前摆脱mysql错误报告生产。如果您的系统发现任何数据库信息出现错误,那就不好了。