dongtiao2105 2010-01-05 00:45
浏览 55

PHP函数返回一个引用变量

I'm trying to write a function that will return a reference to a PDO object. The reason for wanting a reference is if, for some reason, the PDO object gets called twice in one page load, it will just return the same object rather than initializing a new one. This isn't silly, is it? :/

function &getPDO()
{
   var $pdo;

   if (!$pdo)
   {
      $pdo = new PDO...

      return $pdo;
   }
   else
   {
      return &pdo;
   }
}

Something like that

  • 写回答

4条回答 默认 最新

  • dongpang9573 2010-01-05 00:47
    关注

    To make a reference to a variable $foo, do this:

    $bar =& $foo;
    

    Then return $bar.

    评论

报告相同问题?