2013-05-12 71 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