IM學習化的PHP :) 在第一,對不起我的英文不好,我儘量講normaly :)致命錯誤:調用未定義的方法DBConnection的準備::()
我總是先從數據庫編寫一些代碼,但所有的時間都有延伸問題。請幫幫我。
的index.php
define('CWM', TRUE);
define('DS', DIRECTORY_SEPARATOR);
define('PATH', dirname(__FILE__) . DS);
define('LINK', dirname($_SERVER['SCRIPT_NAME']));
require_once 'classes' . DS . 'database.php';
require_once 'classes' . DS . 'session.php';
require_once 'classes' . DS . 'core.php';
$core = new core;
core.php中必須包括會話和DBConnection的類
if(!defined('CWM')) die('script access error');
class core extends session{
protected $db;
function __construct(){
$this->db = new dbconnection();
parent::session();
}
}
database.php中課堂,我試圖連接到數據庫
class dbconnection{
protected $db;
protected $dbinfo = array();
public function connect(){
if(file_exists(PATH . 'classes' . DS . 'config.php')){
$this->dbinfo = require_once PATH . 'classes' . DS . 'config.php';
try{
$this->db = new PDO('mysql:host=' . $this->dbinfo['hostname'] . ';dbname='. $this->dbinfo['dbname'], $this->dbinfo['username'], $this->dbinfo['password'], array(PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC));
return $this->db;
}catch(PDOException $e){
die($e->getMessage());
}
}else{
trigger_error('undefined config.php', E_USER_ERROR);
}
}
function __destruct(){
$this->db = NULL;
}
}
session.php文件如果用戶有會話cookie,這個類從我的bd選擇三個信息
if(!defined('CWM')) die('script access error');
class session extends dbconnection{
protected $db;
protected $member = array();
function __construct(){
parent::connect();
$this->session;
}
protected function session(){
$_COOKIE['session'] = 5;
if(!empty($_COOKIE['session'])){
$this->member = $this->db->prepare("SELECT * FROM `users` WHERE `session` = '?'")->execute(array($_COOKIE['session']));
var_dump($this->member);
}else{
$this->member = false;
}
}
}
如果更多鈔票,我需要的是核心類包括會話和數據庫類,並採取這些會話類包括數據庫和核心類
'堂課擴展DbConnection' **指違反**里氏替換原則和單一職責原理。它也打破了關注的分離。如果你關心代碼的質量和可維護性,你應該谷歌爲這些東西 – Yang 2015-01-21 00:35:47