【问题标题】:Method Chaining PHP方法链 PHP
【发布时间】:2015-06-19 02:16:10
【问题描述】:

我有个快速的问题让我头疼。

我正在尝试使用 PHP 中的方法链制作表单验证系统

我想要做的是能够调用例如(请检查代码cmets):

$firstname = $OBJECT->Forms->Field("First Name", "firstname"); //This one doesn't validate, but just puts what's on firstname field on to $firstname. But this way doesn't work for me, because I have to return the object so it can be chainable and not the variable of the POST. How can I do this?
$firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate(); //this one validates if the field is not empty and if it's empty it'll insert the first parameter ("First Name") onto an array to display the errors.
$email = $OBJECT->Forms->Field("Email", "email")->Validate()->Email(); //This one does the same as above but validates Email and inserts the value of the email field onto $email
but I prefer the next one...
$email = $OBJECT->Forms->Field("Email", "email")->Validate->Email(); //I'd rather prefer this method but I don't know how to do it without using the parenthesis on the Validate method.

我只能让它这样工作

    $firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate();
and
    $firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate()->Email();

没有->Validate();,我似乎无法让它工作(像这样:$firstname = $OBJECT->Forms->Field("First Name", "firstname");

分享的代码有点乱。但是代码很简单……我有一个forms.class.php 和一个validate.class.php。 forms.class.php 从 validate.class.php 创建 Validate 类的实例,Forms 对象通过构造函数上的 Validate 类传递。

我希望能够做到:

$OBJECT->Forms->Field();
$OBJECT->Forms->Field()->Validate();
$OBJECT->Forms->Field()->Validate()->Email;
$OBJECT->Forms->Field()->Validate()->Telephone;

或者这个更喜欢:

$OBJECT->Forms->Field();
$OBJECT->Forms->Field()->Validate;
$OBJECT->Forms->Field()->Validate->Email;
$OBJECT->Forms->Field()->Validate->Telephone;

只是想通了:

$OBJECT->Forms->Field()->Validate();
$OBJECT->Forms->Field()->Validate()->Email();
$OBJECT->Forms->Field()->Validate()->Telephone();

但是任何形式都可以

谢谢。

【问题讨论】:

  • 你的班级是什么样的?
  • 这是一个 PHP 框架,代码中等大小。但如果你愿意,我可以发布它。但我真的很想要一些关于如何实现这一目标的例子。谢谢
  • 这感觉像是相关阅读:stackoverflow.com/q/12351737/2943403

标签: php oop methods chaining


【解决方案1】:

看看这是不是你想要做的:

<?php

    class   FormValidate
        {
            protected   $args;
            public      $valid;

            public  function Forms()
                {
                    // Don't know what this function is supposed to do....
                    return $this;
                }

            public  function Validate()
                {
                    $numargs    =   func_num_args();
                    $this->args =   array();

                    if($numargs == 2) {
                            $vals       =   func_get_args();
                            $this->args[$vals[1]]   =   $vals[0];
                            $this->valid            =   true;
                        }
                    else
                        $this->valid    =   false;

                    if(isset($this->args['firstname']) && !empty($this->args['firstname']))
                        return true;

                    return  $this;
                }

            public  function Email()
                {
                    if(isset($this->args['email'])) {
                            if(filter_var($this->args['email'],FILTER_VALIDATE_EMAIL))
                                return $this->valid     =   $this->args['email'];
                        }

                    return $this->valid =   false;
                }

            public  function Telephone()
                {
                    if(isset($this->args['telephone'])) {
                            if(preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/',$this->args['telephone']))
                                return $this->valid     =   $this->args['telephone'];
                        }

                    return $this->valid =   false;
                }
        }

        $test   =   new FormValidate();
        // These will throw a fatal error on the base Validate('First Name','firstname')
        // if you add another method to the chain like so: ->Validate('First Name','firstname')->Email();
        echo $test->Forms()->Validate('123-876-0987','telephone')->Telephone();

?>

【讨论】:

  • 其实我要做的是:$telephone = $test->Forms("444445555")->Telephone();,那么$telephone的值就是444445555,它必须验证电话();,如果->电话();不存在:然后将 444445555 分配给 $telephone 而不使用 Telephone() 进行验证;
  • 所以你不需要FieldValidate 方法??
  • 嗯,应该是 $test->Forms->Field("First Name", "firstname")->Validate();而 Field 上的 firstname 是表单输入名称。然后 Validate() 将验证它是否为空,但它应该返回名字输入的值,如果该字段为空,Validate() 将添加到另一个特殊变量。与 $test->Forms->Field("Telephone", "telephone")->ValidateTelephone(); 相同,如果电话有效或无效,它应该返回电话,但如果它无效,它将存储在另一个变量中。
  • 和 $test->Forms->Field("First Name", "firstname"); (没有 ->Validate();) 应该返回输入字段值但不验证它。所以结构应该是 $test->Forms->Field("First Name", "firstname"); //返回名字输入字段值 $test->Forms->Field("First Name", "firstname")->Validate(); //返回名字输入字段值,如果名字为空,则添加到特殊变量中。 $test->Forms->Field("Telephone", "telephone")->ValidateTelephone();
  • 其实我想做的是能够做这样的事情 $test->a()->b()->c(); $test->a()->b(); $test->a()->c(); $test->a();
猜你喜欢
  • 1970-01-01
  • 2011-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 2016-02-05
  • 1970-01-01
相关资源
最近更新 更多