简体   繁体   English

验证类的设计正确吗?

[英]Correct design for a validation class?

I'd like to create a class that will validate form input (please no comments about reinventing the wheel). 我想创建一个类来验证表单输入(请不要对重新发明轮子有任何评论)。

I'm thinking it makes more sense to have a class with static validation methods, rather than having to instantiate a validation object since the object wouldn't hold any data? 我认为拥有一个带有静态验证方法的类,而不是必须实例化验证对象是有意义的,因为该对象将不保存任何数据?

So for example, something like: 因此,例如:

<?php

class Validator {

    static function is_numeric($val) {

        if(is_numeric($val))
            return true;

    }

}

Would allow me to use: 请允许我使用:

if(!Validator::is_numeric($_POST['age'])) $error['age'] = "Age must be a number";

Whereas: 鉴于:

<?php

class Validator {

    public function is_numeric($val) {

        if(is_numeric($val))
            return true;

    }

}

Would mean having to instantiate an instance of the class to use the functions - a pointless use of system memory, I'd have thought? 是否意味着必须实例化该类的实例才能使用这些功能-我曾想过对系统内存的无意义使用? :

$validator = new Validator();
if(!$validator->is_numeric($_POST['age'])) $error['age'] = "Age must be a number";

My example validation method is a poor example since it doesn't do anything than use the standard PHP is_numeric function, but this is just for the sake of illustrating my point. 我的示例验证方法是一个糟糕的示例,因为它除了使用标准的PHP is_numeric函数外没有做任何事情,但这只是为了说明我的观点。

Is the static method class the better option? 静态方法类是更好的选择吗? If not, why? 如果没有,为什么?

I would suggest not only making your validation methods static, but also allowing instantiation of the class. 我不仅建议使您的验证方法成为静态方法,还建议允许实例化该类。

Lets pretend we call our class 'Field'. 假设我们将类称为“字段”。 It would then be nice to have a constructor with the arguments: field_name, widget_type, validator_type and whatever else you want. 然后,最好有一个带有以下参数的构造函数:field_name,widget_type,validator_type以及您想要的其他任何参数。 then you can just make Field objects and call 'validate()' or some other similarly named function. 那么您就可以制作Field对象并调用“ validate()”或其他类似名称的函数。

eg: 例如:

$email_field = new Field('Email','entry','email_validator');
$email_field.validate();

The constructor can then fetch it's value from the request variable, and the validate function will look for a static validator called 'email_validator'. 然后,构造函数可以从请求变量中获取其值,并且validate函数将查找名为“ email_validator”的静态验证器。 even better would be to put the validator in the constructor... 更好的是将验证器放在构造函数中...

I think, if you use more than one function calls on a specific instance of your validator class. 我认为,如果您在验证器类的特定实例上使用多个函数调用。 It's best to use a singleton method or just create one instance (ie $validator = new Validator; ); 最好使用单例方法或仅创建一个实例(即$validator = new Validator; );

Memory wise this would be the best choice, I'd say, if you have (let's say) 100 separate static calls to the class instead of 1 instance with 100 calls on that, you'd be better of creating a new instance. 在内存方面,这将是最佳选择,我想说的是,如果对类有100个单独的静态调用(而不是1个实例,其中有100个调用),那么最好创建一个新实例。

Using a validator class seems to be the better choice for me. 对我来说,使用验证器类似乎是更好的选择。 You can use it without instantiation, if there are no class variables needed. 如果不需要类变量,则可以不实例化地使用它。 And if you need no longtime instance of that class, it won't need any space in the RAM. 而且,如果您不需要该类的长时间实例,则在RAM中将不需要任何空间。

Think about the maintainability and readability of your code. 考虑一下代码的可维护性和可读性。 It's always tempting to build frameworks and classes and so on but embedding the same features in 100 LOC instead of 40 isn't always the good choice. 建立框架和类等等总是很诱人的,但是将相同的功能嵌入100 LOC而不是40并不是总是一个好选择。 The decision should depend on what your validator will do. 该决定应取决于您的验证者将执行的操作。

If it has no internal state, and no complex parameters, don't make classes. 如果它没有内部状态,也没有复杂的参数,请不要创建类。

A simple function would not only be simpler to analyze, it would let the user know that there is no side effect, that he doesn't have to read more documentation than the one of the function he intends to use. 一个简单的函数不仅会更易于分析,而且还会让用户知道没有副作用,而且他不必阅读比他打算使用的功能更多的文档。

Keep it simple. 把事情简单化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM