2013-05-12 94 views
0

我有一些麻煩試圖將我的公共類中的$_POST值設置爲屬性

class Sector 
    { 

    private $sectorName; 
    private $sectorInfo; 
    private $sectorCategory; 

    private $errors; 
    private $token; 

    private $config; 

    public function __constructor() 
    { 
     $this->errors = array(); 
     $this->token = $_POST['token']; 

     $this->sectorName  = $_POST['secName']; 
     $this->sectorInfo  = $_POST['secInfo']; 
     $this->sectorCategory = $_POST['secCat']; 

     $this->config = parse_ini_file('config.ini'); 
    } 

的問題是,當我檢查,如果$this->sectorName是空的,它總是返回true。 但是,當我檢查$_POST['secName']是否爲空時,它將返回false

編輯:這是我檢查,如果該屬性是空的功能和error_handler功能

public function show_errors() 
{ 
    echo "Errores: "; 
    foreach($this->errors as $key => $value) 
     echo $value . "<br/>"; 
} 

public function valid_data() 
{ 
    if (empty($this->sectorName)) 
    { 
     $this->errors [] = "Datos incorrectos"; 
    } 

return count($this->errors)? 0 : 1; 
} 
+0

嗯,var_dump $ _POST請 – 2013-05-12 16:33:03

回答

2

我想你是假設公共職能__constructor是一個構造函數中,實際上它是不是你應該使用的魔法功能__construct

public function __construct (
    $this->errors = array(); 
    $this->token = $_POST['token']; 

    $this->sectorName  = $_POST['secName']; 
    $this->sectorInfo  = $_POST['secInfo']; 
    $this->sectorCategory = $_POST['secCat']; 

    $this->config = parse_ini_file('config.ini'); 
} 
+0

謝謝,這是我noob的錯誤! – 2013-05-12 16:43:14