php - 函数中使用的 PDO 范围

标签 php mysql class pdo scope

我正在使用如下结构 -

class foo{
.
.
.

  function bar($colID){
     try{
        $dbo = new PDO(get_db_DSN(), 
                       get_db_USR(), 
                       get_db_PWD());
        $pstmt = $dbo->prepare("SELECT * FROM table_name WHERE col=:colID");
        $pstmt->bindValue(':colID', $colID);
        $pstmt->execute();
        .
        .
        .
     }catch(PDOException e){
      ...
      }
}

我需要知道 PDO 是否会持续存在,直到 foo 的对象存在,或者它是否会在函数作用域结束后立即被销毁。

最佳答案

$dbo之后将被“销毁”。

Variable containers get destroyed when the "refcount" reaches zero. The "refcount" gets decreased by one when any symbol linked to the variable container leaves the scope (e.g. when the function ends) or when unset() is called on a symbol.

Reference Counting Basics

关于php - 函数中使用的 PDO 范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16681528/

相关文章:

c++ - c++中的初学者预期的类名错误

java - 在类之间共享变量

php - 如何在 PHP 中访问数组的第 N 个元素

php - 直接从 Twig 中的 ChoiceType 元素访问特定选项

php - php 死掉后 Bootstrap 下拉列表停止工作

mysql - 使用Mysql从不同名称的不同表中选择值作为贷方和借方并计算余额

MySql 授予错误 : 'Select' is not valid at this position

php - 将数组插入 MySQL DB(意外的 T_STRING)

python - __init__ 方法中的异常处理

Phpexcel 得到错误的日期