【问题标题】:Var_dump doesnt workVar_dump 不起作用
【发布时间】:2013-05-03 12:26:30
【问题描述】:

在这个简单的类中,我想要例如 var_dump 规则。但这不起作用。 有人有想法吗?

好的,我做了一些更改。它在帖子真假后回馈。有用。这段代码是 OOP 的好方法吗?

class Forms_FormsValidation {

private $_required;
private $_minLength;
private $_maxLength;
private $_alphaNumeric;
private $_errors;

public function __construct($validate, $posts) {
    array_pop($posts);
    foreach ($validate as $arraykey => $value) {            
        foreach ($value as $key => $values) {
            if (method_exists($this, "set$key")) {                    
                $set = 'set'.ucfirst($key);
                $get = 'get'.ucfirst($key);
                $this->$set($posts["$arraykey"], $values);
                if( $this->$get() != '') {
                    $this->_errors[$arraykey] .= $this->$get();
                }                    
            }
        }
    }          
}

public function setValidation(){
    if ( empty($this->_errors) ){return TRUE;}return FALSE;
}
public function getRequired() {
    return $this->_required;
}

public function setRequired($value, $ruleValue) {
    if (empty($value) && $ruleValue == TRUE) {
        $this->_required = 'newwwwwwwwww this field is required';
    }
}

public function getMinLength() {
    return $this->_minLength;
}

public function setMinLength($value, $ruleValue) {
    if (strlen($value) < $ruleValue) {
        $this->_minLength = 'must be longer than' . $ruleValue . '';
    }
}

public function getMaxLength() {
    return $this->_maxLength;
}

public function setMaxLength($value, $ruleValue) {
    if (strlen($value) > $ruleValue) {
        $this->_maxLength = 'must be shorter than' . $ruleValue . '';
    }
}

public function getAlphaNumeric() {
    return $this->_alphaNumeric;
}

public function setAlphaNumeric($value, $ruleValue) {
    if (!preg_match('/^([a-z0-9])+$/i', $value)) {
        $this->_alphaNumeric = 'can only contain letters and numbers';
    }
}

}

$validateUser       = array( 'user' => array ( 'required' => TRUE, 'minLength' => 2,'maxLength' => 15, 'alphaNumeric' => TRUE ));
$validatePassword   = array( 'password' => array ( 'required' => TRUE, 'minLength' => 2,'maxLength' => 15, 'alphaNumeric' => TRUE ));
$_POST = array_map('strip_tags', $_POST);
$formsValidation = new Forms_FormsValidation($validateUser, $_POST); 
$formsValidation = new Forms_FormsValidation($validatePassword, $_POST); 
if ( $formsValidation->setValidation() === TRUE ) {echo 'TRUE';}

【问题讨论】:

  • “它不起作用”是什么意思?它应该说“NULL”
  • 您在 __construct() 中使用了 var_dump,其中 _validationRules 为空,因此它不会打印任何内容。

标签: php class var-dump


【解决方案1】:

在构造函数内部调用var_dump($this-&gt;_validationRules); 没有显示任何内容,因为此时$this-&gt;_validationRules 为空。你可以在setValidationRules函数调用之后调用它,它会起作用

【讨论】:

    【解决方案2】:

    var_dump($this-&gt;_validationRules); 不起作用,因为您试图在类的构造中调用它。只有在创建类的实例或对象时才会调用此构造。那个时候$this-&gt;_validationRules不包含任何你需要在var_dump中的值

    setValidationRules()
    

    功能。

    这意味着当你这样做时

    $formsValidation = new Forms_FormsValidation(); 
    

    构造函数被调用,此时它没有任何值。

    试试这个来获取转储结果:

    public function setValidationRules( $validationRules )
    {
        $this->_validationRules = $validationRules;
        var_dump($this->_validationRules);
    }  
    

    【讨论】:

      【解决方案3】:

      Var_dump() 在构造函数中工作正常,问题是你需要在创建对象之前初始化成员。

      例子:

      在构造函数中:

      public function __construct() {
           $this->_validationRules = array( 'required' => TRUE, 'minLength' => 2,'maxLength' => 15, 'alphaNumeric' => TRUE );
           var_dump($this->_validationRules);
      }
      

      或者你仍然可以在类成员定义中定义变量如下

      class Forms_FormsValidation {        
          private $_validationRules = array( 'required' => TRUE, 'minLength' => 2,'maxLength' => 15, 'alphaNumeric' => TRUE );
      

      【讨论】:

      • 谢谢,但我想在另一个页面中设置数组并在类中传递真正的数组并使用它。例如我有用户和密码字段,那么它们都有不同的验证规则
      【解决方案4】:

      在这个函数中使用var_dump

      public function getValidationRules()
      {
          var_dump($this->_validationRules);
      } 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-12-26
        • 1970-01-01
        • 1970-01-01
        • 2020-06-05
        • 2012-08-06
        • 2011-11-24
        • 2013-11-17
        相关资源
        最近更新 更多