将$ _POST分配给公共类PHP的私有属性

时间:2013-05-12 16:30:11

标签: php oop class variables

我在尝试将$_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;
}

1 个答案:

答案 0 :(得分: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');
}