oop - Method Chaining PHP -
i have quick question that's killing head.
i'm trying make form validation system method chaining in php
what want able call example (please check code comments):
$firstname = $object->forms->field("first name", "firstname"); //this 1 doesn't validate, puts what's on firstname field on $firstname. way doesn't work me, because have return object can chainable , not variable of post. how can this? $firstname = $object->forms->field("first name", "firstname")->validate(); //this 1 validates if field not empty , if it's empty it'll insert first parameter ("first name") onto array display errors. $email = $object->forms->field("email", "email")->validate()->email(); //this 1 same above validates email , inserts value of email field onto $email prefer next one... $email = $object->forms->field("email", "email")->validate->email(); //i'd rather prefer method don't know how without using parenthesis on validate method.
i can make work this
$firstname = $object->forms->field("first name", "firstname")->validate(); , $firstname = $object->forms->field("first name", "firstname")->validate()->email();
without ->validate();
can't seem make work (like this: $firstname = $object->forms->field("first name", "firstname");
)
the code kinda mess share. code simple... have forms.class.php , validate.class.php. forms.class.php creates instance of validate class validate.class.php , forms object passed through validate class on constructor.
i want able do:
$object->forms->field(); $object->forms->field()->validate(); $object->forms->field()->validate()->email; $object->forms->field()->validate()->telephone;
or 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 form ok
thank you.
see if trying do:
<?php class formvalidate { protected $args; public $valid; public function forms() { // don't know function supposed 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 throw fatal error on base validate('first name','firstname') // if add method chain so: ->validate('first name','firstname')->email(); echo $test->forms()->validate('123-876-0987','telephone')->telephone(); ?>
Comments
Post a Comment