尝试通过__get()方法访问私有变量将返回null

时间:2012-03-21 11:07:32

标签: php model-view-controller oop

使用PHP 5.3,我目前正在编写一个MVC应用程序,需要提取传递给模型进行验证的数据,然后发送回视图,以便在出错时重新表单。

模型中的字段标记为私有,只有当它们出现在fieldNames列表中时才能被访问(因此控制器不能尝试更改类中其他“业务”字段的内容。

在下面标记为“BUG”的控制器区域中,尝试访问私有字段只会导致创建数组项但设置为null,而不是值。在我的调试器中,如果我检查源的字段($ templateM-> $ field),它会显示正确的值。

发生了什么事?

在模型基类中:

class model {

protected $fieldNames;

public function __get($name)
{
    if ($name === 'fieldNames')
        return $this->fieldNames; // Special exception for getting the list of fields
    if ($name === 'errorList')
        return $this->errorList; // special exception for getting the current errors
    if (in_array($name, (array)$this->fieldNames))
        return $this->$name;
    else
        throw new Exception('Invalid access to private model field');
}
}

在模型中:

class template extends model
{
function __construct()
{
    parent::__construct();
    $this->fieldNames = new immutableArray(array('id', 'etc'));
}

private $id = 0;
private $etc = 1;
}

在控制器中:

class templateManager extends controller
{
    function create()
    {
        // Validate form data
  $templateM = getModel('template');
  $templates = array();
  $bodyData = array();
  $bodyData['showSuccess'] = false;

  $result = $templateM->validate();

  if ($result)
  {
      if ($templateM->save())
      {
          $bodyData['showSuccess'] = true;
      }
  }

  // Load present data (post insert)
  $templates = $templateM->getAllAsArray();

  $bodyData['errorMessages'] = (array)$templateM->errorList;

  $formData = array();
  if (count($bodyData['errorMessages']) > 0)
  {
      foreach($templateM->fieldNames as $field)
            {
                $formData[$field] = $templateM->$field; // <- BUG
            }
  }

  $bodyData['formData'] = $formData;

  $bodyData['templateData'] = $templates;
  $this->_drawPage($bodyData);
}

1 个答案:

答案 0 :(得分:2)

其实我建议您停止滥用__get()。你已经有太多if了,那个列表只会越来越长。最好制作适当的吸气剂和固定剂方法。

AS导致问题的原因:Model::__get()无法访问私有变量。如果您将它们定义为protected,它将起作用。

此外,您可能会发现this rant有用。至少是“旁注”部分。