【发布时间】:2015-01-20 20:46:16
【问题描述】:
我正在学习 php :) 首先,对不起我的英语不好,我试着正常说话:)
我总是开始用数据库编写一些代码,但总是遇到扩展问题。请帮帮我。
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 必须包含 session 和 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,则此类 tri 从我的 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;
}
}
}
如果可能的话,我需要核心类包括会话和数据库类,而会话类包括数据库和核心类
【问题讨论】:
-
class Session extends DbConnection表示违反里氏替换原则和单一责任原则。它还打破了关注点分离。如果你关心代码质量和可维护性,你应该在谷歌上搜索这些东西