更進一步的了解了「多型」、「超級繼承」以及「介面」、虛擬函式等等技術。
所以也同時開發出除了原本針對$_POST,又可以變更成$_GET或是$_REQUEST的做法。
用法:
如果是post的來源:
require("class.get_post_sql.php");
$postdata = new add_post("資料表名稱");
$postdata -> sql_save;
如果是get的來源:
require("class.get_post_sql.php");
$postdata = new add_get("資料表名稱");
$postdata -> sql_save;
如果有post和get的來源:
require("class.get_post_sql.php");
$postdata = new add_request("資料表名稱");
$postdata -> sql_save;
有其他的資料來源(以post為例)
require("class.get_post_sql.php");
$postdata = new add_post("資料表名稱");
$postdata -> add_text("資料欄位","值");
$postdata -> sql_save;
檔名:class.get_post_sql.php
interface add_interface{
function add_text($add_field,$add_value);
function sql_text();
function sql_save();
function dearray();
}
abstract class super_add implements add_interface{
function __construct($table){
$this->table = $table;
$this->dearray();
}
function add_text($add_field,$add_value){
$this->field .= $add_field.",";
$this->data .= "'".htmlentities($add_value,ENT_QUOTES,"utf-8")."',";
}
function sql_text(){
$this->field = substr($this->field,0,strlen($this->field) -1);
$this->data = substr($this->data,0,strlen($this->data) -1);
return "INSERT INTO `".$this->table."` (".$this->field.")
VALUES (".$this->data.")";
}
function sql_save(){
mysql_query($this->sql_text()) or die("NO SAVE");
}
}
class add_get extends super_add{
function dearray(){
foreach($_GET as $key => $value){
if ($key != "submit" && $key != "MM_insert" && $key != "button") {
$this->add_text($key,$value);
}
}
}
}
class add_post extends super_add{
function dearray(){
foreach($_POST as $key => $value){
if ($key != "submit" && $key != "MM_insert" && $key != "button") {
$this->add_text($key,$value);
}
}
}
}
class add_request extends super_add{
function dearray(){
foreach($_REQUEST as $key => $value){
if ($key != "submit" && $key != "MM_insert" && $key != "button") {
$this->add_text($key,$value);
}
}
}
}

dearray 裡面重復的程式碼該重構吧!
回覆刪除