迭代PHP中的类

时间:2014-05-27 08:53:45

标签: php class

我有一个与数据库交互的包装器。为数据库中的每个表自动生成一个类,这扩展了一个基类,该基类包含与数据库交互的函数,例如: viewTable,viewForm,getRecordByID ...它的一个关键目标是使用自动完成功能,因此我不必记住字段名称等,并使代码更安全,更不容易出错。

除了一点点之外我对它非常满意:在我的代码中,为了生成一个HTML表单,我使用这样的代码:

$objInvoice = new cls__Invoice();

$objInvoice->fldID->Active(true); //Active means it will appear on the form
                                  //and a POST will be expected
$objInvoice->fldCompany_ID->Active(true);
$objInvoice->fldSomeValue->Active(true);

$objInvoice->initForm(); //Read posted variables

echo $objInvoice->viewForm(); //viewForm can be overloaded to suit the project

我可以使用许多其他设置:

$ objInvoice-> fldCompany_ID->标题('公司参考');

但问题出现在这里:

$ objInvoice-> fldSomeValue->的inputType(' MoneyInput&#39);

我传入输入类型以允许使用特定的输入标记或下拉列表等,并允许以特定方式读取它。例如。 " ip地址"类型实际上显示4个输入,它们之间有一个点。因此,在保存到数据库之前,必须将其拆分为显示和连接。

同样,这一切都非常好,但是我的基类中间有两个很难看的开关语句。我想找到一个解决方案;

  1. 很优雅
  2. 允许读取部分和视图部分在代码中彼此相邻
  3. 允许开发人员创建自己的输入类型,而不会弄乱核心类。
  4. 允许我在此位使用自动完成:...-> InputType($ objSomething-> MoneyInput);
  5. 我已经提出了一些解决方案,但他们在上面的第1点失败了。我怀疑解决方案将是这样一个类:

    class clsInputBase {
        var $InputType;
    
        public function Read($objField) {
            $result = $_POST[$objField->VariableName];
            return $result;
        } //funct
    
        public function Output($objField) {
            //Standard code for outputing a normal one
            //NB: SIMPLIFIED FOR EXAMPLE!
            $result = '<input value="' . $objField->Value() . '" id="' . $objField->VariableName() . '"';
            //...
            $result .= '/>' . "\r\n";
    
            return $result;
        } //funct
    } //funct
    

    然后我会为每个输入类型扩展这个类,并根据需要重载这两个函数 然后真正的问题开始了:我需要找到一种方法来遍历基类中的对象/类,以及开发人员为数组或其他东西添加自定义的方法。 所以我想我要么创建一个类名数组,然后使用$ type =&#39; myclass&#39 ;;创建该类的对象。 $ instance = new $ type; (不是很优雅)或创造许多我可能不需要的物品(也不是很优雅)。

    对不起,这太长了 有什么建议吗?

2 个答案:

答案 0 :(得分:1)

我会做以下事情:

// can be extended
class Form
{
// class variable to hold all inputs
// class construct expects form config and an array with inputs
// Methods to create forms, parsing forms
}

// can be extended
class Input
{
// class variables to hold input configuration with default values
// methods to echo, parse and validate inputs
// class construct expects array with single input config
// hooks to manipulate with data before and after all logics
}

然后:

$formParams = array(...);
$formInputs = array(
    input configurations
);

$form = new Form($formParams, $formInputs);
$form->initForm();

然后Form类通过每个$ formInputs值进行迭代,并使用相应的配置创建新的Input类对象。

在活动记录类中,当它生成时,您可以为每列提供默认输入配置。

现在,扩展这个很容易,假设我们需要自定义输入类型:

class CustomInput extends Input
{
    //at construct we call parent constuctor and adding our own data or manipulating with old
    //if we want to transform data, we can use hooks
    //this class objects still will be available at Form class object 
}

您可以在输入配置中指定自定义类,或者您可以删除第二个配置数组并手动添加输入。

最后我们将获得统一的表单类,我们可以立即使用: 在许多情况下,您甚至可以考虑不重载Input类,而是在基类中引入灵活的逻辑,因此它可以接受许多配置选项。

答案 1 :(得分:0)

查看Iterator界面:

class myIterator implements Iterator {
    private $position = 0;
    private $array = array(
        "firstelement",
        "secondelement",
        "lastelement",
    );  

    public function __construct() {
        $this->position = 0;
    }

    function rewind() {
        var_dump(__METHOD__);
        $this->position = 0;
    }

    function current() {
        var_dump(__METHOD__);
        return $this->array[$this->position];
    }

    function key() {
        var_dump(__METHOD__);
        return $this->position;
    }

    function next() {
        var_dump(__METHOD__);
        ++$this->position;
    }

    function valid() {
        var_dump(__METHOD__);
        return isset($this->array[$this->position]);
    }
}

$it = new myIterator;

foreach($it as $key => $value) {
    var_dump($key, $value);
    echo "\n";
}

http://www.php.net/manual/de/class.iterator.php