写一个连接类似的案例,试一下用命名空间实现,并进行类的自动加载。
数据库名为:demo
表格名为:user1
Db.php Db类
<?php
namespace model;
//创建单例模式数据库访问类
class Db
{
//数据库配置信息
private $dbConfig = [
'name' => 'demo', //数据库名称
'dbType' => 'mysql',//数据库类型
'host' => 'localhost',//数据库地址
'userName' => 'root',//数据库用户名
'password' => 'root',//数据库密码
'charset' => 'utf8'//字符集
];
public $insertId = null;//记录新增最后一条ID
public $num = 0;//用于记录影响数据行数
private $pdo = null;//pdo对象
private static $instance = null; //唯一实例
//实例化Db类
public static function getInstance($param = [])
{
if (!self::$instance instanceof self) {
self::$instance = new self($param);
}
return self::$instance;
}
//使用用户自定义参数,替换默认参数,连接数据库
private function __construct($param = [])
{
$this->dbConfig = array_merge($this->dbConfig, $param);
$this->connect();
}
//私有化克隆
private function __clone()
{
// TODO: Implement __clone() method.
}
// 数据库的连接
private function connect()
{
try {
$dsn = $this->dbConfig['dbType'] .
':host=' . $this->dbConfig['host'] .
';dbname=' . $this->dbConfig['name'] .
';charset=' . $this->dbConfig['charset'];
$this->pdo = new \PDO($dsn, $this->dbConfig['userName'], $this->dbConfig['password']);
} catch (DOMException $e) {
echo $e->getMessage();
exit();
}
}
//数据库操作增删改
public function exec($sql)
{
$num = $this->pdo->exec($sql);
if ($num > 0) {
if ($this->pdo->lastInsertId() !== null) {
$this->insertId = $this->pdo->lastInsertId();
}
$this->num = $num;
} else {
print_r($this->pdo->errorInfo());
}
}
//查询一条语句
public function getOne($sql){
$pdoStmt=$this->pdo->query($sql);
if (isset($pdoStmt)&&$pdoStmt->rowCount()==1) {
return $pdoStmt->fetch(\PDO::FETCH_ASSOC);
}else {
return( $pdoStmt->errorInfo());
}
}
//查询全部
public function getAll($sql){
$pdoStmt=$this->pdo->query($sql);
if (isset($pdoStmt)&&$pdoStmt->rowCount()>0) {
return $pdoStmt->fetchAll(\PDO::FETCH_ASSOC);
}else {
print_r( $pdoStmt->errorInfo());
}
}
}Model.php Model 类
<?php
namespace model;
class Model
{
protected $db=null;
public $data=0;
public function __construct()
{
$this->init();
}
public function init(){
$dbConfig=[
'usernanme'=>'root',
'password'=>'root',
'dbname'=>'demo'
];
$this->db=Db::getInstance($dbConfig);
}
public function getOne($id=1){
$sql="SELECT * FROM user1 WHERE id={$id}";
$this->data=$this->db->getOne($sql);
return $this->data;
}
public function getAll(){
$sql="SELECT * FROM user1 ";
$this->data=$this->db->getAll($sql);
return $this->data;
}
}ModelStudent.php ModelStudent 类
<?php
namespace model;
class ModelStudent extends Model
{
}UserController.php
<?php
include "../loader.php";//自动加载类
class UserController
{
public function getAll(){
$stu=new \model\ModelStudent();
$data=$stu->getAll();
// print_r($data);
include '../view/getAll.php';
}
public function getOne($id){
$stu=new \model\ModelStudent();
$data=$stu->getOne($id);
// print_r($data);
include '../view/getOne.php';
}
}
(new UserController)->getAll();Loader.php
<?php
function my_autoloader($class) {
include $class . '.php';
}
spl_autoload_register('my_autoloader');
//$test=new \model\Test();//测试getAll.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script> <title>Title</title> </head> <body> <div class="container"> <div class="row"> <div class="col-sm-12"> <table class="table table-bordered table-hover"> <h2 class="text-center">用户信息表</h2> <tr> <th>ID</th> <th>名字</th> <th>邮箱</th> <th>密码</th> </tr> <?php foreach ($data as $row):?> <tr> <td><?php echo $row['id']?></td> <td><?php echo $row['name']?></td> <td><?php echo $row['email']?></td> <td><?php echo sha1($row['password'])?></td> </tr> <?php endforeach ?> </table> </div> </div> </div> </body> </html>

getOne.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script> <script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script> <title>Title</title> </head> <body> <div class="container"> <div class="row"> <div class="col-sm-12"> <table class="table table-bordered table-hover"> <h2 class="text-center">用户信息表</h2> <tr> <th>ID</th> <th>名字</th> <th>邮箱</th> <th>密码</th> </tr> <?php foreach ($data as $row):?> <td><?php echo $row ?></td> <?php endforeach ?> </table> </div> </div> </div> </body> </html>

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号