2011-03-23 65 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