【问题标题】:assign $_POST to private property of a public class PHP [closed]将 $_POST 分配给公共类 PHP 的私有属性 [关闭]
【发布时间】:2013-05-12 16:30:11
【问题描述】:

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

【问题讨论】:

  • 嗯,请 var_dump $_POST

标签: php oop class variables


【解决方案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');
}

【讨论】:

  • 谢谢,这是我的菜鸟的错误!
猜你喜欢
  • 1970-01-01
  • 2018-08-13
  • 2015-06-09
  • 2013-10-06
  • 2014-03-26
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多