2013-05-09 340 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