我有這樣的代碼(兩班)
class url
{
private $profile_id;
private $short;
public $notice;
private $forbidden;
function url() {
$this->forbidden = array('index.php', 'index.html', 'styles.css', 'style.css');
if ($_POST['profile_id']){
// global $db;
$exists = db::fetch_one(db::query("SELECT count(1) FROM ".TABLE." WHERE profile_id = ".intval($_POST['profile_id']).";"));
$exists_username = db::fetch_one(db::query("SELECT count(1) FROM ".TABLE." WHERE url_short = '".db::mres($_POST['url_short'])."'"));
}
}
}
class db
{
function db(){
mysql_connect("localhost", "root", "h1gh§c1a0");
mysql_select_db("gplus") or die(mysql_error());
mysql_set_charset("utf8") or die(mysql_error());
}
function query($query){
// print_r($this);
$result = mysql_query(self::protect($query)) or _log("Query failed: ".$query);
//$this isn't working
//$result = mysql_query($this->protect($query)) or _log("Query failed: ".$query);
return $result;
}
function fetch($result){
$result = mysql_fetch_assoc($result);
return $result;
}
function fetch_one($result){
$result = mysql_fetch_row($result);
return $result['0'];
}
function mres($text) {
return mysql_real_escape_string($result);
}
function protect($text) {
if (preg_match("/UNION/i", $text)) {
_log("Hack attempt: ".$text);
die();
}
// die($text);
return $text;
}
}
$db = new db();
$url = new url();
我的問題是,這條線
$result = mysql_query(self::protect($query)) or _log("Query failed: ".$query);
的作品,但是當我改變self::
與$this->
它拋出錯誤
Fatal error: Call to undefined method url::protect() in /data/my/db.php on line 71
這怎麼可能?我認爲$this->function();
在當前類中調用方法。我究竟做錯了什麼?
'$ this'指向對象的當前實例。你使用'db'作爲靜態類,因此沒有實例。使用'self'很好。你爲什麼堅持使用'$ this'? – kba
我正在創建瀏覽器遊戲,我從來沒有使用靜態類。但我喜歡 - > $ this->;) – genesis