在这个类层次结构中哪里可以关闭mysql连接?

发布于 2024-11-26 02:16:13 字数 1407 浏览 1 评论 0原文

我在 PHP 中有 4 个类用于注册用户,问题是我不知道如何关闭 mysql 连接。

类调用:

single_connect->database->post->signup

当我在单例中实现 mysql_close() 时,它会破坏我的代码。我的假设是,只要创建了注册对象,它所扩展的类也会被“实例化”。但事实似乎并非如此。

我必须注释掉 mysql_close 才能使其正常工作。请注意,我的单例使用数据库链接来确定它是否存在,而不是像大多数单例一样使用指向自身的指针。

/*single_connect*/

class single_connect
  {
  private static $_db_pointer = NULL;
  private function __destruct() 
    {
    //mysql_close();
    }
  private function __construct()
    {
    self::$_db_pointer = mysql_connect(DB_HOST, DB_USER, DB_PASS);
    mysql_select_db(DB_DATABASE);
    }
  public static function get_connection()
    {
    if(self::$_db_pointer == NULL)
      {
      return new self();
      } 
    }
  }

/*database*/

abstract class database
  {
  protected function __construct()
    {
    single_connect::get_connection();
    }
  protected static function query($query)
    {
    $result = mysql_query($query) or die(mysql_error());
    return $result;
    }
  }

/*post*/

class post extends database
  {
  public $_protected_arr=array();
  protected function __construct()
    {
    parent::__construct();
    $this->protect();
    }
  protected function protect()
    {
    foreach($_POST as $key => $value)
      {
      $this->_protected_arr[$key] = mysql_real_escape_string($value);
      }
    }
  }

/*signup*/

class signup extends post 
  { 
 ...

I have 4 classes in PHP I use to signup a user, problem is I don't know how to close the mysql connection.

Class calls:

single_connect->database->post->signup

When I implement mysql_close() in the singleton it breaks my code. My assumption is that as long as a signup object is created the classes it extends from are also "instantiated". But this does not appear to be the case.

I had to comment out the mysql_close to allow this to work. Note that my singleton uses the database link to determine if it exists wrather then a pointer to itself like most Singletons.

/*single_connect*/

class single_connect
  {
  private static $_db_pointer = NULL;
  private function __destruct() 
    {
    //mysql_close();
    }
  private function __construct()
    {
    self::$_db_pointer = mysql_connect(DB_HOST, DB_USER, DB_PASS);
    mysql_select_db(DB_DATABASE);
    }
  public static function get_connection()
    {
    if(self::$_db_pointer == NULL)
      {
      return new self();
      } 
    }
  }

/*database*/

abstract class database
  {
  protected function __construct()
    {
    single_connect::get_connection();
    }
  protected static function query($query)
    {
    $result = mysql_query($query) or die(mysql_error());
    return $result;
    }
  }

/*post*/

class post extends database
  {
  public $_protected_arr=array();
  protected function __construct()
    {
    parent::__construct();
    $this->protect();
    }
  protected function protect()
    {
    foreach($_POST as $key => $value)
      {
      $this->_protected_arr[$key] = mysql_real_escape_string($value);
      }
    }
  }

/*signup*/

class signup extends post 
  { 
 ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枕头说它不想醒 2024-12-03 02:16:13

脚本执行完成后,连接将自动关闭。因此,如果最后一次数据库操作和脚本结束之间没有很长的时间,我就不会显式关闭连接。

Connections are automagically being closed when script execution finishes. So if there is no tremendous amount of time between the last database operation and the end of the script I would not bother to explicitly close the connection.

日久见人心 2024-12-03 02:16:13

我发现了问题。在我对 Singleton 类和静态属性的解释中,我检查了 PHP 如何处理静态属性。

“与任何其他 PHP 静态变量一样,静态属性只能使用文字或常量进行初始化;不允许使用表达式。因此,虽然您可以将静态属性初始化为整数或数组(例如),但不能将其初始化为另一个变量、函数返回值或对象。” http://php.net/manual/en/language.oop5.static.php

您无法像在 C++、Java 或大多数其他 OOP 语言中那样将静态属性设置为对象。在这种情况下,当类被 GC 时,连接被关闭。

我向 prodigitalson 道歉,我的评论不正确。

I found the problem. In my explanation about Singleton classes and static properties I checked how PHP handles static properties.

"Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object." http://php.net/manual/en/language.oop5.static.php

You cannot set the static property as an object like you can in C++, Java or most other OOP languages. In this case the connection was being closed when the class was GC'ed.

My apologies to prodigitalson, I was incorrect in my comments.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文