2012-08-08 3 views
0
abstract class User 
{ 
    protected $content; // $content object will be assigned here 


} 

class Member extends User 
{ 

} 


abstract class Content 
{ 
function create() 
{ 
    //create some content 
} 
} 

class Text extends Content 
{ 

} 

class Image extends Content 
{ 
} 

Abstract User은 사용자 정의 할 수있는 롤을 기반으로 다른 클래스로 확장 할 수 있습니다. 각 롤은 Content 클래스를 사용할 수있는 다른 권한을 가지며 하위 클래스 메서드입니다. 각 메서드마다 수동으로 각 권한을 쓸 수는 있지만 동적으로 수행하려고합니다.다른 클래스에서 호출하는 동안 클래스의 메서드를 제한하는 중

어떻게하면됩니까?

답변

1

귀하의 질문에 두 번째 독서 시간에 UPDATE

은, 난 그냥 더 잘 맞는 당신이 필요위한 또 다른 방법을 생각했습니다. php가 다중 상속을 지원하지 않지만 특성에 php> = 5.4가 필요한 경우, 두 번째 세대 " (다른 레벨의 하위)을 추가하는 것이 좋습니다. 추상 클래스는 모두 final 키워드로 사용 가능한 메소드 만 포함하거나 라인 바깥쪽에 1 개의 변형 만있는 경우 (이 경우 final 키워드를 삭제하고 멤버 함수를 무시). 내가 전에 말했듯

,이 물건을 설명하는 좋은 아니에요, 그래서 앞으로 수업 모습 수있는 방법의 넣어 함께 예를했다 :

abstract class User 
{ 
    public function __construct() 
    { 
     echo get_class($this).'<br/>'; 
    } 
    final function forAll() 
    { 
     echo 'access for all'; 
    } 
} 
class TxtGroup extends User 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
    } 
    public function abstr() 
    { 
     echo 'TxtGroup specific'; 
    } 
} 
class ImgGroup extends User 
{ 
    public function __construct() 
    { 
     echo 'Same, but different for ImgGroup<br/>'; 
     parent::__construct(); 
    } 
    public function abstr() 
    { 
     echo 'ImgGroup specific'; 
    } 
} 
class inst1 extends TxtGroup 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     if ($this instanceof TxtGroup) 
     { 
      echo 'Child member of: TxtGroup<br/>'; 
     } 
     if ($this instanceof inst1) 
     { 
      echo 'Child instance of inst1 (itself)<br/>'; 
     } 
     if ($this instanceof User) 
     { 
      echo 'grand-Child of User<br/>'; 
     } 
    } 
    public function objSpecific() 
    { 
     echo 'self explanatory<br/>'; 
    } 
} 
class inst2 extends ImgGroup 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     if ($this instanceof ImgGroup) 
     { 
      echo 'Child member of: ImgGroup<br/>'; 
     } 
     if ($this instanceof inst2) 
     { 
      echo 'Child insance of inst2 (itself)<br/>'; 
     } 
     if ($this instanceof User) 
     { 
      echo 'grand-Child of User<br/>'; 
     } 
    } 
} 
$foo = new inst1(); 
$bar = new inst2(); 

무엇을보기를 I 평균? 출력 :

INST1
자식 멤버의 : TxtGroup ImgGroup
INST2
자식 멤버의에 대한 INST1의
아이 인스턴스 (자체)
사용자
동일의 그랜드 아이,하지만 서로 다른 : ImgGroup 사용자의 INST1의
아동 insance (자체)
그랜드 아이


최신 버전의 PHP를 사용하고 있다면 그 특징이 있습니다. 어떤 클래스가 memberfunction을 호출하는지 확인할 수있는 많은 방법이 있습니다. 가장 쉬운 방법은, IMO, get_class($this);의 값을 선택하여 방법을 시작하는 것입니다 :

abstract class Foo 
{ 
    public function who() 
    { 
     echo get_class($this); 
    } 
} 
class bar extends Foo 
{ 
    public function __construct() 
    { 
     $this->who(); 
    } 
} 
$f = new bar(); 

에코의 bar, 당신은 추상적 인 방법을 변경하고, 필요한 경우 예외를 던질 수 있습니다. 내가에 그것을 확인하지만

abstract class Foo 
{ 
    public function who() 
    { 
     $child = explode('_',get_class($this)); 
     if (end($child) === 'whoExec') 
     { 
      return $this->whoExec(); 
     } 
     echo 'this isn\'t for bar the bar instance'; 
     return $this; 
    } 
    private function whoExec() 
    { 
     if(!strstr('whoExec',get_class($this))) 
     { 
      return $this->who(); 
     } 
     echo 'This methods mimics overloading -sort of'; 
     return $this; 
    } 
} 
class bar_whoExec extends Foo 
{ 
    public function __construct() 
    { 
     $this->who(); 
    } 
} 
$f = new bar(); 
+0

내가, 특성을 사용하는 방법을 잘하지 않았다 :

같은 구조는 특정 메소드를 오버로드 할 수 있습니다,이 (끔찍한) 예처럼 보여줍니다 (/ 표시하려고합니다) 조작. 나는 C++/java의 배경을 가지고 있으므로 형질의 실제 사용을 확신하지 못했습니다. 아이디어를 가져 주셔서 감사합니다. – varuog

+0

@ user1538127 : 방금 전에 5.4 버전보다 더 간단한 대안을 추가했습니다. 추신 : C++/java에 배경이있는 경우 특성을 부분 상속과 같은 것으로 생각하십시오. –

관련 문제