2011-12-26 2 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 
    } 

} 

요청이 아약스 인 경우 편집 할 수 없기를 바랍니다. 실패하면 $layout은 어린이가 편집 할 수 있어야합니다.조건에 따라 자식이 편집하는 변수 양식을 방지합니다.

어린이를위한 변수 readonly을 만드는 방법이 있습니다. 세트/get 메소드와 변수 비공개 불구하고

+0

무엇을 당신의 아약스 요청 "날씨"관계 되는가를

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

는 읽기 전용 누구나 읽을 액세스 할 수 있지만, 단지 클래스에 쓸 수있다? :) – mark

답변

0

한 가지 방법 것은 비공개로 자신의 가시성을 설정 한 다음 마법 방법을 사용하는 것입니다 __get 및 __set을 사용하여 액세스하십시오. 예를 들면 다음과 같습니다

1

당신은 멤버 변수에 대한 액세스를 제어 할 수 있습니다 : readOnly 인 특성을 얻을 수

class ParentClass 
{ 
    private $requestType = "Normal"; 

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

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