我在PHP OOP上相当新,但下面的一段PHP代码返回以下错误,我不明白为什么。如果有人能指出可能是一个明显的错误,我将非常感激
致命错误:在C:\ tester
中调用未定义函数get_column_names()class tester
{
public $database_name;
public $auto_increment_col;
public $foreign_key_array;
public $column_names;
function __construct($wpdb,$tablename)
{
$this->database_name = get_database_name();
$this->auto_increment_col = get_auto_increment_field($tablename);
$this->foreign_key_array = get_foreign_keys($tablename);
//$this->column_names = $wpdb->get_results( "SHOW COLUMNS FROM $tablename");
$this->column_names = get_column_names($wpdb,$tablename);
}
protected function get_column_names($wpdb,$tablename)
{
return $wpdb->get_results( "SHOW COLUMNS FROM $tablename");
}
protected function get_database_name()
{
$r = mysql_query("SELECT DATABASE()") or die(mysql_error());
return mysql_result($r, 0);
}
protected function get_auto_increment_field($tablename)
{
$sql = "describe $tablename";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result))
{
if ($row['Extra'] == 'auto_increment')
{
return($row['Field']);
}
}
return(null);
}
protected function get_foreign_keys($wpdb,$tablename)
{
$foreign_keys = array();
$sql = "select *
FROM information_schema.TABLE_CONSTRAINTS i
LEFT JOIN information_schema.KEY_COLUMN_USAGE k ON i.CONSTRAINT_NAME = k.CONSTRAINT_NAME
WHERE i.CONSTRAINT_TYPE = 'FOREIGN KEY'
AND i.TABLE_SCHEMA = DATABASE()
AND i.TABLE_NAME = '$tablename'";
$rows = $wpdb->get_results($sql);
if (count($rows) == 0)
return(null);
else
return($rows);
}
}
答案 0 :(得分:4)
调用对象方法时缺少$this
:
$this->column_names = $this->get_column_names($wpdb,$tablename);
// ... and so on