代码之家  ›  专栏  ›  技术社区  ›  Josh K

PHP类问题

  •  1
  • Josh K  · 技术社区  · 15 年前

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

        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 = "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 回复  |  直到 15 年前
        1
  •  4
  •   Felix Kling    15 年前

    function 关键词。一定是这样

    class Lead extends ModelBase
    {
        public function Lead()
        {
    
        }
    
        public function insert()
        {
            //....
        }
    }
    
        2
  •  3
  •   philfreo    15 年前

    PHP5 constructor syntax :

       public function __construct() { ... }
    

       public Lead() { ... }
    

    function 反正是关键词。

        3
  •  2
  •   danielrsmith    15 年前

    加上:

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

    到测试页面的顶部。

        4
  •  0
  •   Eino T    15 年前

    记住,在投入生产之前,要清除mysql错误报告。如果您的系统在发生错误时泄露了任何数据库信息,那就不好了。