2011-03-23 42 views
1
<?php 
$mysql_host='mysql1.000webhost.com'; 
$mysql_dbname='a8130617_skola'; 
$mysql_username='something'; 
$mysql_password='something'; 

class mysql { 
    try{ 
    public $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_dbname", 
      $mysql_username, $mysql_password); 
    } 
    catch(PDOException $e){ 
     echo $e->getMessage(); 
    } 
} //ERROR EXCLAMATION MARK HERE??? 
?> 

为什么netbeans 6.9.1认为这是错误的语法? 非常感谢php pdo mysql连接 - 混淆语法问题

回答

0
try{ 
public $db = new PDO("mysql:host=$mysql_host;dbname=$mysql_dbname", 
     $mysql_username, $mysql_password); 
} 
catch(PDOException $e){ 
    echo $e->getMessage(); 
} 

尝试捕获块需要一个方法内。但随着这一点,不知道为什么你要在课堂上包装这个?你的类是一个已经定义的类的包装。

+0

没有错,假设它正确完成。很高兴不必一遍又一遍地重写怪异的PDO连接字符串。 – 2011-03-23 20:15:40

+0

就是这样..那好吧。你是对的,当我有一个叫做参数的概念时,为什么我会把它包装在类中?非常感谢 – 2011-03-23 20:23:58

+0

有些东西叫做常量,包括等等。这些都可以让它简单。下面的例子并不能为你节省很多。我唯一要做的就是让你的连接对象变成静态的,否则我不会看到像这样包装它的好处。从长远来看,您最终会将编码翻一番。 – mardala 2011-03-23 23:22:37

4

你知道OOP的事吗?

类应包含字段和/或方法。你只是用class{}包围了一段代码。这不是编程。

阅读关于OOP在PHP - 这里是手动:http://php.net/manual/en/language.oop5.php

读它自己的好处。

编辑:

我知道下面的例子可以让你多懒,但我会采取一拍,相信你会读到更多。

实施例类的连接可以看起来像:

class Mysql { 

    protected $_host; 
    protected $_dbname; 
    protected $_username; 
    protected $_password; 
    protected $_db; 

    public function __construct($host = null, $dbname = null, $username = null, $password = null) 
    { 
     $this->_host = $host; 
     $this->_dbname = $dbname; 
     $this->_username = $username; 
     $this->_password = $password; 
    } 

    public function connect() 
    { 
     try { 
      $this->_db = new PDO('mysql:host=' . $this->_host . ';dbname=' . $this->_dbname, $this->_username, $this->_password); 
     } 
     catch(PDOException $e){ 
      echo $e->getMessage(); 
     } 
    } 

    public function getDb() 
    { 
     return $this->db; 
    } 

    public function setHost($host) 
    { 
     $this->_host = $host; 
     return $this; 
    } 

    public function getHost() 
    { 
     return $this->_host; 
    } 

    public function setDbname($dbname) 
    { 
     $this->_dbname = $dbname; 
     return $this; 
    } 

    public function getDbname() 
    { 
     return $this->_dbname; 
    } 

    public function setUsername($username) 
    { 
     $this->_username = $username; 
     return $this; 
    } 

    public function getUsername() 
    { 
     return $this->_username; 
    } 

    public function setPassword($password) 
    { 
     $this->_password = $password; 
     return $this; 
    } 

    public function getPassword() 
    { 
     return $this->_password; 
    } 


} 

而且示例用法:

$mysql = new Mysql('mysql1.000webhost.com', 'a8130617_skola', 'something', 'something'); 
$mysql->connect(); 
+0

这是一个很好的负责任的编程演示。谢谢 – 2011-03-23 20:55:52

+0

我的想法是:你为什么需要这个?如果你有$ db = new PDO(.. CREDS ...);与$ db = new MySQL();它并没有真正节省你的时间,并增加了另一个不必要的层。在这种情况下,具体而言。如果有人担心必须输入连接字符串,那么就把它放在一个常量或其他不可变的变量中。 – mardala 2011-03-24 22:27:04

+0

这是在这种情况下正确使用'class'和OOP的表示。 – hsz 2011-03-25 08:00:15