class example {
private $_db;
function __construct(){
include "./Lib/Db.php";
$this->_db = new Db("localhost","root","123456","test");
}
function getList(){
$this->_db->query("......");//这里具体sql语句就省略不写了
}
}
class Factory {
public static function getDb(){
include "./Lib/Db.php";
return new Db("localhost","root","123456","test");
}
}
class example {
private $_db;
function __construct(){
$this->_db = Factory::getDb();
}
function getList(){
$this->_db->query("......");//这里具体sql语句就省略不写了
}
}
class example {
private $_db;
function getList(){
$this->_db->query("......");//这里具体sql语句就省略不写了
}
//从外部注入db连接
function setDb($connection){
$this->_db = $connection;
}
}
//调用
$example = new example();
$example->setDb(Factory::getDb());//注入db连接
$example->getList();
$example->setDb(Factory::getDb());//注入db连接 $example->setFile(Factory::getFile());//注入文件处理类 $example->setImage(Factory::getImage());//注入Image处理类 ...
class Factory {
public static function getExample(){
$example = new example();
$example->setDb(Factory::getDb());//注入db连接
$example->setFile(Factory::getFile());//注入文件处理类
$example->setImage(Factory::getImage());//注入Image处理类
return $expample;
}
}
$example=Factory::getExample(); $example->getList();
class example {
private $_di;
function __construct(Di &$di){
$this->_di = $di;
}
//通过di容器获取db实例
function getList(){
$this->_di->get('db')->query("......");//这里具体sql语句就省略不写了
}
}
$di = new Di();
$di->set("db",function(){
return new Db("localhost","root","root","test");
});
$example = new example($di);
$example->getList();
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有