Method Chaining PHP

634 views Asked by At

I have a quick question that's killing my head.

I'm trying to make a Form Validation System with Method Chaining in PHP

What I want to do is to be able to call for example (please check the code comments):

$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.

I can only make it work like this

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

Without ->Validate(); I can't seem to make it work (Like this: $firstname = $OBJECT->Forms->Field("First Name", "firstname");)

The code is kinda mess to share. But the code is simple... I have a forms.class.php and a validate.class.php. The forms.class.php creates an instance of Validate class from validate.class.php and the Forms Object is passed through the Validate class on the constructor.

I want to be able to do:

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

or this preferebly:

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

Only figured out:

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

But any form is OK

Thank you.

1

There are 1 answers

5
Rasclatt On

See if this is what you are trying to do:

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

?>