2013-05-09 229 views
0

在这个简单的类中,我想要var_dump这个规则。但这没有用。 有没有人有想法?Var_dump does not work

好的,我做了一些改变。它发布后真实或错误的回报。有用。这段代码是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';} 
+2

你是什么意思“,它并没有定义在类成员变量定义变量工作“?它应该说“NULL” – 2013-05-09 05:15:06

+0

您已在__construct()中使用var_dump,其中_validationRules为空,因此它不会打印任何内容。 – CRDave 2013-05-09 05:15:38

回答

1

使用var_dump这个功能

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

var_dump($this->_validationRules);不能正常工作,因为您正试图在类的构造中调用它。只有在创建实例或类的对象时才会调用此构造。那个时候$this->_validationRules不包含在

setValidationRules() 

功能需要var_dump任何价值。

这意味着,当你这样做

$formsValidation = new Forms_FormsValidation(); 

的构造函数被调用,当时它在它没有价值。

试试这个用于获取转储结果:

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

调用var_dump($this->_validationRules);内部构造没有显示任何东西作为$this->_validationRules是在这一点空。你也可以在setValidationRules函数调用调用它,它会工作

0

的var_dump()在构造函数中工作正常,问题是u需要在创建之前初始化成员目的。

例子:

在构造函数中:

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); 
+0

谢谢,但我想在另一个页面中设置数组,并传递true类中的数组并使用它。例如,我有用户和密码字段,然后他们有两个不同的验证规则 – Bas 2013-05-09 05:38:29