作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑 3 个类 DBCONNECT、book、new
class dbconnect {
protected $db_conn;
public $db_user='xxxx';
public $db_pass='xxxx';
public $db_host='localhost';
public $db_name='xxxx';
function connect() {
try{
$this->db_conn=new PDO("mysql:host=$this->db_host;dbname=$this->db_name",$this->db_user,$this->db_pass);
return $this->db_conn;
}
catch (Exception $e){
return $e->getMessage();
}
}
}
include_once ( 'class.dbconn.php' );
class Book{
public $link;
public function __construct(){
$db_conn=new dbconnect();
$this->link = $db_conn->connect();
return $this->link;
}
}
class new{
include_once 'classes/class.book.php';
$book = new Book();
}
一切正常,我的代码没有错误,但我怀疑是否要关闭与我的数据库的连接。是否必须关闭连接?如果是必须的,那我该如何关闭连接,代码需要写在哪个类中?
最佳答案
手动关闭连接不是强制性的。 PHP 会自行处理。如果你想手动关闭它,你可以使用 $this->db_conn = null;
public function disconnect() {
$this->db_conn = null
}
如果你想成为一个完美主义者,你也可以这样做,虽然不是必须的
public function __destruct() {
$this->disconnect();
}
关于php - 如何在 OOPS 中的 php mysql 中断开或关闭与 DB 的数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18929305/
我是一名优秀的程序员,十分优秀!