2

对于一个项目,我使用了一个名为 meekrodb 的 mysql 库,我想用我自己的 mysql 函数制作一个扩展类,所以如果更新了库,我只需重新编辑扩展类。

作为一名初级程序员,我尝试按如下方式扩展库:

require './libs/meekrodb.2.0.class.php';
class Database_extend extends MeekroDB  {
protected $db;
protected $database='';

function __construct($server=null, $user=null, $pass=null, $database=null)
{   
    // Creating a database object

$this->db= parent::__construct();  
    //$this->db= parent::__construct($server,$user,$pass,$database);  
    $this->database=$database;

} */
 public function table_exists($tableName=null) {
 if(is_null($tableName )){ 
 trigger_error("Error in: ".__METHOD__." Missing table name.",E_USER_ERROR);     
 }
 else{
$sql = "SELECT COUNT(*) AS count FROM information_schema.tables WHERE table_schema = '$this->database' AND table_name = '$tableName'";
$exists=$this->db->query($sql);
if($exists['count']>0)
{  return TRUE;}
else { return FALSE;}
}  
}#-table_exists

如果我测试这个类(test.php)

 require './config.inc.php';
 require './libs/Session.php';
 require_once './libs/Ngram.class.php';   
 require_once './libs/Database_extend.class.php';
 /*require_once './libs/meekrodb.2.0.class.php';*/

 DB::$user=DB_USER;
 DB::$password=DB_PASS;
 DB::$dbName=DB_DATABASE;
 $db1=new Database_extend(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
 $t=$db1->query("SELECT * FROM temp LIMIT 0,1");
 //var_dump($t); //THIS WORKS
 var_dump($db1->table_exists('tem')); // THIS DOESN'T work

我收到“在非对象上调用成员函数 query()”错误,这意味着 db 变量不是对象。

我怎样才能正确解决这个问题?

4

1 回答 1

0

The first thing that comes to mind is that you don't pass on the initialization parameters here:

$this->db= parent::__construct();  

Your override method receives $server, $user, $pass, $database, but you don't make them available to the original code. Hencewhy it won't correctly set up the $db property.

Also you overwrite $this->db. But the parent constructor would certainly assign it itself.

于 2012-06-06T23:39:27.910 回答