面向对象的 PHP 保护属性在 foreach 循环中不可用? [英] OO PHP protected properties not available in foreach loop?

查看:43
本文介绍了面向对象的 PHP 保护属性在 foreach 循环中不可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个验证类,我想用它来检查应用程序中的所有值是否在允许的约束范围内.

我正在将一个对象传递给验证类中的静态函数,来自另一个类(在本例中为 User)

function validate() {$errors = Validation::validate($this);}

在验证类中,我创建了一个新对象,然后继续处理传递的参数对象的属性(或者至少这是我想要做的).

function validate($object) {$validation = new Validation();print_r($object);print_r('<br/>');foreach($object as $key => $val) {打印_r($val);isset($val->maxlength) ?$validation->validateLengthNoMoreThan($val->value, $val->maxlength) : null;isset($val->minlength) ?$validation->validateLengthAtLeast($val->value, $val->minlength) : null;isset($val->required) &&($val->required == true) ?$validation->validateNotBlank($val->value) : null;}返回 $validation->errors;}

我在函数内打印值纯粹是为了测试目的.我不明白的是为什么对象在 foreach 循环之外打印得很好,但是如果我尝试访问循环内的值,则不会显示任何内容.

这是在 foreach 循环之外显示的内容:

<前>用户对象 ([用户名:受保护] => 属性对象( [值] => aaa [最大长度] => 12 [最小长度] => 3 [必需] => 1)[firstname:protected] =Property Object ( [value] => aaa [maxlength] => 12 [minlength] => 3 [required] => 1 )[lastname:protected] =Property Object ( [value] => aaa [maxlength] => 12 [minlength] => 3 [required] => 1))

验证类不扩展 User 类,所以我理解为什么这些值不可用,只是不明白为什么它们在循环外可用但在循环内不可用.

此外,对受保护/私有属性进行验证的最佳方法是什么?

任何建议/提示将不胜感激.

谢谢.

解决方案

您可以通过对实际对象内的属性进行 foreach 来解决此问题.所以每个对象都必须有一个 validate() 函数,你可以用一个接口来强制执行.例如:

class MyClass 实现 Validator {$var1;$var2;public function validate(){//Validator 接口需要$validation = new Validation();foreach($this as $k=>$v) {//运行验证}返回 $validation->errors;}}

I have a validation class which I would like to use to check all values in my application are within allowed constraints.

I am passing an object to a static function within the validation class, from another class (in this case User)

function validate() {
    $errors = Validation::validate($this);
}

In the validation class, I create a new object and then proceed through the properties of the passed parameter object (or at least that is what I would like to do).

function validate($object) {
            $validation = new Validation();
            print_r($object);
            print_r('<br />');
            foreach($object as $key => $val) {
                print_r($val);
                isset($val->maxlength) ? $validation->validateLengthNoMoreThan($val->value, $val->maxlength) : null;
                isset($val->minlength) ? $validation->validateLengthAtLeast($val->value, $val->minlength) : null;
                isset($val->required) && ($val->required == true) ? $validation->validateNotBlank($val->value) : null;
            }
            return $validation->errors;
        }

I am printing out values within the function purely for test purposes. What I don't understand is why the object prints out fine outside of the foreach loop, but if I try to access the values within the loop, nothing is displayed.

This is what is displayed OUTSIDE of the foreach loop:

User Object ( 
[username:protected] => Property Object ( [value] => aaa [maxlength] => 12 [minlength] => 3 [required] => 1 ) 
[firstname:protected] =Property Object ( [value] => aaa [maxlength] => 12 [minlength] => 3 [required] => 1 )
[lastname:protected] =Property Object ( [value] => aaa [maxlength] => 12 [minlength] => 3 [required] => 1 ) 
) 

The validation class does NOT extend the User class, so I understand why the values would not be available, just not why they are available outside of the loop but not inside of it.

Also, what would be the best way to carry out validation on protected/private properties?

Any advice/tips would be greatly appreciated.

Thanks.

解决方案

You can get around this issue by foreach'ing the properties inside the actual object. So each object must have a validate() function, which you could enforce with an interface. E.g.:

class MyClass implements Validator {
  $var1;
  $var2;

  public function validate(){ //required by Validator interface
     $validation = new Validation();
     foreach($this as $k=>$v) {
        //run validations
     }
     return $validation->errors;     
  }
}

这篇关于面向对象的 PHP 保护属性在 foreach 循环中不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆