将$_POST赋值给PHP公共类的私有属性


assign $_POST to private property of a public class PHP

我有一些麻烦试图将$_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

EDIT:这里是我检查属性是否为空的函数和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;
}

我想你是假设公共函数__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');
}