我收到错误: 使用$这个时候不是在为$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