方法链接PHP
我有一个很快就要问的问题,真让我头疼 我正在尝试用PHP中的方法链制作一个表单验证系统 例如,我想做的是能够调用(请检查代码注释): 我只能让它像这样工作方法链接PHP,php,oop,methods,chaining,Php,Oop,Methods,Chaining,我有一个很快就要问的问题,真让我头疼 我正在尝试用PHP中的方法链制作一个表单验证系统 例如,我想做的是能够调用(请检查代码注释): 我只能让它像这样工作 $firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate(); and $firstname = $OBJECT->Forms->Field("First Name", "firstname")->Vali
$firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate();
and
$firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate()->Email();
没有->Validate()代码>我似乎无法让它工作(像这样:$firstname=$OBJECT->Forms->Field(“firstname”,“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
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();
?>
你的类看起来像什么?它是一个PHP框架,代码是中等大小的。但是如果你愿意,我可以把它寄出去。但我真的想举一些例子来说明如何做到这一点。谢谢这感觉像是一个相关的阅读:实际上我想做的是:$telephone=$test->Forms(“444445555”)->telephone();,然后,$telephone的值为44444 5555,它必须验证电话();,如果->电话();不存在:然后将444 5555分配给$telephone,而不使用telephone()进行验证;因此,您不需要字段
或验证
方法??好的,它将是$test->Forms->Field(“firstname”,“firstname”)->Validate();而字段上的firstname是表单输入名称。然后Validate()将验证它是否为空,但它应该返回first name输入的值,如果字段为空,则Validate()将添加到另一个特殊变量。与$test->Forms->Field(“电话”、“电话”)->ValidateTelephone();,相同;,如果电话有效或无效,它应该返回电话,但如果无效,它将存储在另一个变量中;(不带->验证();)应返回输入字段值,但不验证它。所以结构应该是$test->Forms->Field(“firstname”,“firstname”)//返回firstname输入字段值$test->Forms->field(“firstname”、“firstname”)->Validate()//返回firstname输入字段值,如果firstname为空,则将其添加到特殊变量$测试->表单->字段(“电话”,“电话”)->验证电话();实际上,我想做的是能够像这样做$test->a()->b()->c()$测试->a()->b()$测试->a()->c()$测试->a();
<?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();
?>