防止子窗体根据条件编辑变量表单


prevent a variable form being edited by child based on condition

class AppController extends Controller {
/**
 * @var $IsAjax weather request is ajax or not
 */
public $RequestType = 'NORMAL'; //make this read only by child
    function __construct() 
    {
      if(isAjax())
        $RequestType ='AJAX'; 
        $layout = 'ajax'; //this var should not be editable further i.e read only by child
    }
}

如果请求是ajax,我希望它是不可编辑的。如果失败,$layout应该可以被子节点编辑。

还有一种方法可以使变量readonly为child.

您可以通过set/get方法控制对成员变量的访问,并将该变量设置为私有:

class ParentClass
{
    private $requestType = "Normal";
    protected function getRequestType()
    {
        return $this->requestType;
    }
    protected function setRequestType( $newType )
    {
        if ( $this->requestType != "AJAX" )
        {
            $this->requestType = $newType;
        }
    }
};

获取readOnly属性的一种方法是将其可见性设置为private,然后使用魔术方法__get和__set来访问它们。例:

class A{
  private $readOnly = 'readOnly';
  function __get($key){
    if( $key === 'readonly')
      return $this->{$key};
  }
}

那么readonly可以被任何人读取,但是只有A类可以写