方法链接PHP

时间:2015-06-19 02:16:10

标签: php oop methods chaining

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

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

我想做的是能够打个电话(请查看代码注释):

$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();

但任何形式都可以

谢谢。

1 个答案:

答案 0 :(得分: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();

?>