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