[英]Fatal error: Allowed memory size error in php due to bad code
I am quite familiar with PHP but I have just started doing a bit of object oriented stuff with it. 我对PHP非常熟悉,但是我刚刚开始用它做一些面向对象的工作。 I wanted to make a singleton database connection but I ran into a problem and error."Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 523800 bytes) in" I know that I am not supposed to run out of memory the query I am running is 我想建立一个单例数据库连接,但遇到一个问题和错误。“致命错误:允许的内存大小为134217728字节,已用尽(试图分配523800字节),”我知道我不应该用完内存。我正在运行的查询
$con = getConnection();
$stmt = $con->prepare("SELECT gene_name,jgi_protein_id FROM jgi_transcriptid_proteinid_match where our_protein_id = ?");
Here is the code for the class. 这是该类的代码。
class Connection
{
// Store the single instance of connection
private static $connection;
private function __construct()
{
$connection = new mysqli(HOSTNAME, DBUSER, PASSWORD, DBNAME);
if ($connection->connect_errno)
die("Failed to connect to MySQL: (" . $connection->connect_errno . ") " . $connection->connect_error);
}
public static function getInstance()
{
if (!self::$connection)
self::$connection = new Connection();
return self::$connection;
}
public function prepare($query)
{
$statement = $this->prepare($query);
return $statement;
}
}
I am using mysqli for the database stuff. 我正在使用mysqli作为数据库内容。
There are a couple of issues in this code: 这段代码有两个问题:
Infinite recursion 无限递归
public function prepare($query)
{
$statement = $this->prepare($query);
return $statement;
}
Referencing a local variable, instead of the static one 引用局部变量,而不是静态变量
The code should probably reference self::$connection
. 该代码可能应该引用self::$connection
。 Based on the class, though, I'm not sure, as self::$connection
is used in a different way in getInstance()
, which I don't see called anywhere. 但是,基于类,我不确定,因为self::$connection
在getInstance()
中以不同的方式使用,在任何地方都看不到它。
private function __construct()
{
$connection = new mysqli(HOSTNAME, DBUSER, PASSWORD, DBNAME);
if ($connection->connect_errno)
die("Failed to connect to MySQL: (" . $connection->connect_errno . ") " . $connection->connect_error);
}
Naming confusion 命名混乱
The class is called Connection
, and it contains a static variable named $connection
, which stores the singleton instance of the class. 该类称为Connection
,它包含一个名为$connection
的静态变量,该变量存储该类的单例实例。 The constructor contains another $connection
, which is instead a mysqli connection. 构造函数包含另一个$connection
,它是一个mysqli连接。
Refactored class - UNTESTED FIX 重构类-未测试的修复
The class below has not been tested and is provided for illustrative purpose. 以下类别未经测试 ,仅供参考。 Use at your own risk. 使用风险自负。
class Connection { // Store the single instance of the class private static $instance; class Connection {//存储类的单个实例private static $ instance; // Store the mysqli connection private $connection; //存储mysqli连接private $ connection;
public function __construct() {
// NOTE: it would be better to pass connection parameters as arguments
$this->connection = new mysqli(HOSTNAME, DBUSER, PASSWORD, DBNAME);
if ($this->connection->connect_errno)
die("Failed to connect to MySQL: (" . $this->connection->connect_errno . ") " . $this->connection->connect_error);
}
public static function getInstance() {
if(empty(self::$instance)) {
self::$instance = new Connection();
}
return self::$instance;
}
public function prepare($query) {
$statement = $this->connection->prepare($query);
return $statement;
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.