http://www.phpschool.com/gnuboard4/bbs/board.php?bo_table=tipntech&wr_id=71516&sca=%BE%CB%B0%ED%B8%AE%C1%F2



게시판을 만들면서 어떻게 하면 로그인폼을 처리하는 Class와 분리시킬 수 있을까 고민했습니다.
결국 이벤트프로그램의 addEventListener에서 힌트를 얻어 비슷하게 처리할 수 있도록 만들어 봤습니다.
하다보니 로그인폼 처리 Class 뿐만 아니라 Class들의 강한결합상태를 약화시킬 수 있을것도 같네요.

  1 <?php 
  2 class MyObject{ 
  3
  4 private $starr = array();
  5
  6 public function __construct(){} 
  7
  8 //메쏘드 또는 함수 등록
  9 public function addStateListener($state, $method, $object=null){ 
 10 $idx = count($this->starr);
 11 $this->starr['state'][$idx] = $state;
 12 $this->starr['object'][$idx] = $object;
 13 $this->starr['method'][$idx] = $method;
 14 } 
 15
 16 //콜백 실행
 17 protected function executeState($state/*,...*/){ 
 18
 19 $args = func_get_args();
 20 array_shift($args); //첫번째 인자인 state는 args에서 제외시킨다
 21
 22 if(($idx = array_search($state, $this->starr['state'])) < 0) return;
 23
 24 if(is_object($this->starr['object'][$idx])){ //콜백이 오브젝트이면 (오브젝트 우선)
 25 call_user_method_array($this->starr['method'][$idx], $this->starr['object'][$idx], $args);
 26 }else{ //콜백이 함수이면
 27 call_user_func_array($this->starr['method'][$idx], $args);
 28 } 
 29 } 
 30
 31 public function __destruct(){} 
 32 } 
 33
 34
 35 //====================================================
 36 // 예제 1 함수를 호출
 37 //====================================================
 38 class Board extends MyObject{ 
 39
 40 private $is_login = false; //예를 위해서 강제로 로그아웃 상태로 만듬
 41
 42 function write(){ 
 43 if(!$this->is_login){ 
 44 $this->executeState('needlogin', '게시판에 쓸려면 가입해라!');
 45 }else{ 
 46 echo "글을 씁시다";
 47 } 
 48 } 
 49 } 
 50
 51 function showMessage($str){ 
 52 echo "<script>alert('".$str."');</script>";
 53 exit;
 54 } 
 55
 56 $b = new Board();
 57 $b->addStateListener('needlogin', 'showMessage');  //콜백 함수를 등록한다
 58
 59 //자 글을 등록해보자
 60 $b->write();
 61
 62
 63
 64 //====================================================
 65 // 예제 2 클래스의 메쏘드를 호출
 66 //====================================================
 67 class Board extends MyObject{ 
 68
 69 private $is_login = false; //예를 위해서 강제로 로그아웃 상태로 만듬
 70
 71 function write(){ 
 72 if(!$this->is_login){ 
 73 $this->executeState('needlogin');
 74 }else{ 
 75 echo "글을 씁시다";
 76 } 
 77 } 
 78 } 
 79
 80 //로그인 폼 처리 클래스
 81 class LoginProcess{ 
 82
 83 //귀찮아서 html 그냥 씁니다.
 84 public function showbox(){ 
 85 echo <<< HEREDOC
 86 <table border=1> 
 87 <tr> 
 88 <td>아 이 디 : <input type="text" name="mb_id"></td> 
 89 </tr> 
 90 <tr> 
 91 <td>비밀번호 : <input type="password" name="mb_pw"></td> 
 92 </tr> 
 93 <tr> 
 94 <td><input type="submit" value="로그인"></td> 
 95 </tr> 
 96 </table> 
 97 HEREDOC;
 98 } 
 99 } 
100
101 $b = new Board();
102 $l = new LoginProcess();
103 $b->addStateListener('needlogin', 'showbox', $l);
104
105 //자 글을 등록해보자
106 $b->write();
107 ?> 


'JS HTML PHP' 카테고리의 다른 글

DOM 기초  (0) 2011.12.12
웹 폰트 web font  (0) 2011.11.21
PHP 객체 생성자와 객체 파괴자  (0) 2011.11.13
로그인 Text area에 내용입력등의 기본 글자 넣기  (0) 2011.11.10
HTML 풍선 도움말, 줄바꿈 효과  (0) 2011.11.10

설정

트랙백

댓글