我收到錯誤: 使用$這個時候不是在爲$this->filterArray
PHP類存儲表中的數據,列名+型
對象上下文所以我改變這self::filterArray
,我得到錯誤:未知:非靜態方法Abstract :: filterArray()不應該被靜態調用
我不知道我是否有這個正確的,或者如果我甚至應該使用Abstract或Interface?
基本上什麼,我試圖做的是建立一個array(column_name => type)
,這樣我可以這樣使用它來建立與強制數據類型的基本插入:
$cols = SentDAO::describe();
foreach ($cols as $col => $type) {
if (!isset($data[$col])) continue;
switch ($type) {
case self::INT: $value = intval($data[$col]);
case self::TEXT: $value = $this->escape($data[$col]);
case self::DATE_NOW: $value = 'NOW()';
}
$return[] = " {$col} = '{$value}'";
}
我不想最終創造數百個不同的對象,並希望保持它簡單的存儲在一個數組中。
/**
* Abstract
*
*/
abstract class AccountsAbstract
{
/**
* Data Types
*/
const INT = "INT";
const TEXT = "TEXT";
const DATE_NOW = "DATE_NOW";
/**
* Get columns with data type
* @param array $filter: exclude columns
* @return array
*/
abstract static function describe($filter);
/**
* Filter from array, by unsetting element(s)
* @param string/array $filter - match array key
* @param array to be filtered
* @return array
*/
protected function filterArray($filter, $array)
{
if($filter === null) return $array;
if(is_array($filter)){
foreach($filter as $f){
unset($array[$f]);
}
} else {
unset($array[$filter]);
}
return $array;
}
}
class AccountsDAO extends AccountsAbstract
{
/**
* Columns & Data Types.
* @see AccountsAbstract::describe()
*/
public static function describe($filter)
{
$cols = array(
'account_id' => AccountsAbstract::INT,
'key' => AccountsAbstract::TEXT,
'config_id' => AccountsAbstract::INT
);
return $this->filterArray($cols, $filter);
}
}
/**
* Records
*/
class AccountsRecordsDAO extends AccountsAbstract
{
public static function describe($filter)
{
$cols = array(
'record_id' => AccountsAbstract::INT,
'created' => AccountsAbstract::DATE_NOW,
'customer_id' => AccountsAbstract::INT
);
return $this->filterArray($cols, $filter);
}
}
/**
* Config
*/
class AccountsConfigDAO extends AccountsAbstract
{
public static function describe($filter)
{
$cols = array(
'config_id' => AccountsAbstract::INT,
'hidden' => AccountsAbstract::INT,
'language_id' => AccountsAbstract::INT
);
return $this->filterArray($cols, $filter);
}
}
而且我想使用完整的類名使得代碼非常混亂且便攜性較差:AccountsAbstract::INT
,有沒有使用self::INT
替代方式?或者,我應該創建這些作爲私人財產,即使他們永遠不會改變只引用。
聽起來像是你需要花一些時間瞭解哪些情況是,什麼叫吧'靜態「的意思。 – 2013-03-18 08:51:00