2011-12-26 30 views
0
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

+0

那麼你的Ajax請求與「天氣」做什麼? :) – mark 2011-12-26 15:43:41

回答

0

一種方法來獲得只讀屬性是其可見性設置爲私有,然後用魔術方法__get和__set來訪問它們。爲前:

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

然後只讀可訪問由任何人讀取,但只有A類可寫它

1

您可以控制訪問成員變量,雖然設置/ get方法,使私有變量:

class ParentClass 
{ 
    private $requestType = "Normal"; 

    protected function getRequestType() 
    { 
     return $this->requestType; 
    } 

    protected function setRequestType($newType) 
    { 
     if ($this->requestType != "AJAX") 
     { 
      $this->requestType = $newType; 
     } 
    } 
};