2013-08-09 5 views
0

기본적으로 아래 함수를 포함하는 간단한 데이터베이스 클래스가 있으므로 동일한 작업을 수행하는 신중한 클래스를 만들기로 결정했습니다. 특히 사용자 및 암호 클래스에 어떤 기능이 필요합니까? 생산 환경에 대비하기 위해데이터베이스 연결 클래스에 대한 생각

$db = new DB; 
$link = $db->connect()->getLink(); 

class DB { 

    public $connection = array(); 

    public function __construct() { 
     return $this; 
    } 

    public function connect($host=null, $user=null, $pass=null, $database=null) { 
     $this->connection = new Connection($host, $user, $pass, $database); 
     $this->connection->connect(); 
     $this->link = $this->connection->getLink(); 
     if ($this->connection->ping()) { 
      if (!is_null($database)) { 
       if (!$this->connection->databaseConnect($database)) { 
        throw new\Exception('Unable to connect to the server.'); 
       } 
      } 
     }else{ 
      throw new \Exception('Unable to connect to the server.'); 
     } 
     return $this; 
    } 

    public function getLink() { 
     return $this->link; 
    } 

} 

class Connection { 

    protected $chost='localhost'; 
    protected $cuser='guest'; 
    protected $cpass='password'; 
    protected $cdatabase='PROFORDABLE'; 

    function __construct($host=null, $user=null, $pass=null, $database=null) { 

     $host = !is_null($host) ? $host : $this->chost; 
     $user = !is_null($user) ? $user : $this->cuser; 
     $password = !is_null($pass) ? $pass : $this->cpass; 
     $database = !is_null($database) ? $database : $this->cdatabase; 

     $this->set('host', $host)->set('user', $user)->set('password', $password)->set('database', $database); 
     return $this; 
    } 

    public function connect() { 
     $link = mysqli_connect($this->host->getHost(), $this->user->getUser(), $this->password->getPassword()); 
     if (!is_object($link)) { 
      throw new \Exception("An error has occurred while connecting to the server."); 
     } 
     $this->link = $link; 
    } 

    public function databaseConnect($database) { 
     if (!mysqli_select_db($this->getLink(), $database)) { 
      throw new \Exception("Unable to select the database."); 
     } 
    } 

    public function getLink() { 
     return $this->link; 
    } 

    public function ping() { 
     if (mysqli_ping($this->link)) { 
      return TRUE; 
     } 
     return FALSE; 
    } 

    public function set($name, $param) { 
     if (!isset($name) || !isset($param)) return $this; 
     $class = __NAMESPACE__.'\\'.ucwords($name); 
     $this->$name = new $class($param); 
     return $this; 
    } 

    public function get($name) { 
     $getfunc = 'get'.ucwords($name); 
     return $this->$name->$getFunc(); 
    } 

} 

class Host { 
    public function __construct($host) { 
     $this->setHost($host); 
    } 
    public function setHost($host) { 
     $this->host = $host; 
    } 
    public function getHost() { 
     return $this->host; 
    } 
} 

class User { 

    public function __construct($user) { 
     $this->setUser($user); 
    } 

    public function setUser($user) { 
     $this->user = $user; 
    } 

    public function getUser() { 
     return $this->user; 
    } 

} 

class Password { 

    public function __construct($password) { 
     $this->setPassword($password); 
    } 

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

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

    public function sha($value) { 
     return sha1($value); 

    } 

} 

class guestPassword extends Password { 
    const PASSWORD='password'; 
    public function __construct() { 
     return PASSWORD; 
    } 
} 

class Database { 

    public function __construct($database) { 
     $this->setDatabase($database); 
    } 

    public function setDatabase($database) { 
     $this->database = $database; 
    } 

    public function getDatabase() { 
     return $this->database; 
    } 

} 

class Query { 


} 
+1

왜 객체 모드에서 mysqli가 직접적으로 발생하지 않습니까? – GordonM

답변

0

보통 DB 클래스는 연결을 보유합니다. 별도의 연결 클래스를 만드는 것은 과도한 설계입니다.

DB의 API에 대해 내부 정보보다 더 걱정할 것입니다.

+0

구체적으로 API 란 무엇입니까? – user1205600

+0

인터페이스입니다. 'DB'의 공개 기능. – Halcyon

0

여러 연결을 추적하는 데 도움이 되나요 ??

class DB { 

public $connections = array(); 
public $threads = array(); 
public $lastThread=null; 

public function __construct() { 
    $this->connect(); 
    return $this; 
} 

public function connect($host=null,$user=null,$pass=null,$db=null) { 
    $connection = new Connection($host, $user, $pass, $db); 
    if (!$connection->ping() || !$connection->databaseConnect($connection->get('database'))) { 
     throw new \Exception('ping or database select problem'); 
    } 

    $threadId = mysqli_thread_id($connection->getLink()); 
    $this->connections[$threadId] = $connection; 
    $this->threads[] = $threadId; 
    $this->lastThread = $threadId; 
    return $this; 
} 

public function getLink($threadId=null) { 
    if (is_null($threadId)) { 
     if (!isset($this->lastThread) 
     return $this->connections[$this->lastThread]->getLink(); 

    }else{ 
     return $this->connections[$threadId]->getLink(); 
    } 
    } 

} 

class Connection { 

public $link; 
public $host='localhost'; 
public $user='guest'; 
public $pass='password'; 
public $database='PROFORDABLE'; 

function __construct($host=null, $user=null, $pass=null, $database=null) { 
    if (!is_null($host)) $this->host = $host; 
    if (!is_null($user)) $this->user = $user; 
    if (!is_null($pass)) $this->pass = $pass; 
    if (!is_null($database)) $this->database = $database; 
    $this->connect($this->host, $this->user, $this->pass); 
    $this->databaseConnect($this->database); 
    return $this; 
} 

public function connect() { 
    $link = mysqli_connect($this->host, $this->user, $this->pass); 
    if (!is_object($link)) { 
     throw new \Exception("Not object"); 
    } 
    $this->link = $link; 
} 

public function databaseConnect($database=null) { 
    if (!is_null($database)) $this->database = $database; 
    if (!mysqli_select_db($this->getLink(), $this->database)) { 
     return FALSE; 
    } 
    return TRUE; 
} 

public function getLink() { 
    return $this->link; 
} 

public function ping() { 
    if (mysqli_ping($this->link)) { 
     return TRUE; 
    } 
    return FALSE; 
} 

public function set($param, $value) { 
    $this->$param = $value; 
} 

public function get($param) { 
    return $this->$param; 
} 

} 

$db = new DB; 
$link = $db->getLink(); 
$res = mysqli_query($link, 'SELECT TITLE, DESCRIPTION FROM PRO_CONTENT ORDER BY DATE ASC LIMIT 3'); 
$content=array(); 
while ($row = mysqli_fetch_assoc($res)) { 
    $content[] = $row; 
} 

var_dump($content); 
관련 문제