위치 : http://kr1.php.net/manual/en/function.call-user-func-array.php
<?php
Class Delegate
{
private $arrInstances = array();
protected function addObject( $oElement )
{
// add one element on the end of the stack //
$this->arrInstances[] = $oElement;
}
public function __call( $strMethod, $arrParams )
{
// for each element in instance //
foreach( $this->arrInstances as $oElement )
{
// get the class of the element //
$strClass = get_class( $oElement );
// get all methods of the class //
$arrMethods = get_class_methods( $strClass );
// case the method exists into this class //
if( in_array( $strMethod , $arrMethods ) )
{
// prepare caller //
$arrCaller = Array( $strClass , $strMethod );
// return the result of the method into the object //
return call_user_func_array( $arrCaller, $arrParams );
}
}
// any object has the method //
// throw a exception //
throw new Exception( " Method " . $strMethod . " not exist in this class " . get_class($this ) . "." );
}
}
class Log
{
public function sayHi()
{
print "hi!" . "<br/>\n";
}
public function sayMyName()
{
print "log" . "<br/>\n";
}
}
class Other
{
public function sayHello()
{
print "hello there!" . "<br/>\n";
}
public function sayMyName()
{
print "other" . "<br/>\n";
}
}
class Example extends Delegate
{
public function __construct()
{
$this->addObject( new Log() );
$this->addObject( new Other() );
}
}
$oExample = new Example();
$oExample->sayHi();
$oExample->sayHello();
$oExample->sayMyName();
/*
hi!<br/>
hello there!<br/>
log<br/>
*/
?>
댓글 6개
11년 전
이거 어디라 사용하실려구요? 그냥 매직함수 __call 사용한 것 뿐인데...
11년 전
$this->addObject( new Log() );
$this->addObject( new Other() );
이부분.......상속말고 다른 Class 의 Method 을 자신의 Method 처럼 사용하는 부분입니다.
급할땐 용이하죠...
$this->addObject( new Other() );
이부분.......상속말고 다른 Class 의 Method 을 자신의 Method 처럼 사용하는 부분입니다.
급할땐 용이하죠...
11년 전
아~ ^^ 그건 그렇죠 ... new 새로 메모리에 공간을 개설하면 메모리 작으면 좀 버벅 거릴겁니다.
11년 전
저 코드는 메모리에 차지하는 용량을 무시해도 될 정도의 코드예요.....ㅠ_ㅠ
워프는 콜리앗 ... class 로 만든 저 샘플은 개미 발에 미세먼지
혹 new 로 만드는게 버벅일꺼라 생각하시는거라면.......
Framework 쪽 한번 보시는것을 추천 드립니다.
워프는 콜리앗 ... class 로 만든 저 샘플은 개미 발에 미세먼지
혹 new 로 만드는게 버벅일꺼라 생각하시는거라면.......
Framework 쪽 한번 보시는것을 추천 드립니다.
11년 전
Framework 쪽으로 하고는 있습니다. 사이트는 가벼워야하니까 ^^
11년 전
제가 만들어 쓰고 있는 EventDispatcher 라는 클래스와 비슷하네요.
예를 들어 게시판에 로그인이 필요한 부분에는 Dispatcher 만 해주고 관련 처리는 외부 함수를 addEventHandler 메소드로 연결해 주기만 하면 되죠.
이런걸 Observer 패턴이라고 하등가... 머 그럴겁니다.
예를 들어 게시판에 로그인이 필요한 부분에는 Dispatcher 만 해주고 관련 처리는 외부 함수를 addEventHandler 메소드로 연결해 주기만 하면 되죠.
이런걸 Observer 패턴이라고 하등가... 머 그럴겁니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 8230 | 9년 전 | 133 | ||
| 8229 | 9년 전 | 122 | ||
| 8228 |
커네드커네드
|
9년 전 | 162 | |
| 8227 | 9년 전 | 208 | ||
| 8226 | 9년 전 | 223 | ||
| 8225 | 9년 전 | 200 | ||
| 8224 | 9년 전 | 202 | ||
| 8223 | 9년 전 | 175 | ||
| 8222 |
|
9년 전 | 248 | |
| 8221 | 9년 전 | 157 | ||
| 8220 | 9년 전 | 188 | ||
| 8219 | 9년 전 | 157 | ||
| 8218 | 9년 전 | 195 | ||
| 8217 |
star3840
|
9년 전 | 164 | |
| 8216 | 9년 전 | 237 | ||
| 8215 | 9년 전 | 182 | ||
| 8214 | 9년 전 | 292 | ||
| 8213 | 9년 전 | 248 | ||
| 8212 | 9년 전 | 156 | ||
| 8211 | 9년 전 | 330 | ||
| 8210 | 9년 전 | 332 | ||
| 8209 | 9년 전 | 403 | ||
| 8208 | 9년 전 | 299 | ||
| 8207 | 9년 전 | 300 | ||
| 8206 |
|
9년 전 | 252 | |
| 8205 | 9년 전 | 233 | ||
| 8204 | 9년 전 | 212 | ||
| 8203 | 9년 전 | 285 | ||
| 8202 | 9년 전 | 202 | ||
| 8201 | 9년 전 | 248 | ||
| 8200 | 9년 전 | 251 | ||
| 8199 | 9년 전 | 263 | ||
| 8198 | 9년 전 | 235 | ||
| 8197 | 9년 전 | 224 | ||
| 8196 | 9년 전 | 632 | ||
| 8195 | 9년 전 | 230 | ||
| 8194 | 9년 전 | 344 | ||
| 8193 | 9년 전 | 243 | ||
| 8192 | 9년 전 | 266 | ||
| 8191 | 9년 전 | 211 | ||
| 8190 | 9년 전 | 210 | ||
| 8189 | 9년 전 | 269 | ||
| 8188 | 9년 전 | 204 | ||
| 8187 | 9년 전 | 208 | ||
| 8186 | 9년 전 | 207 | ||
| 8185 | 9년 전 | 375 | ||
| 8184 | 9년 전 | 164 | ||
| 8183 | 9년 전 | 389 | ||
| 8182 | 9년 전 | 245 | ||
| 8181 | 9년 전 | 193 | ||
| 8180 | 9년 전 | 774 | ||
| 8179 | 9년 전 | 548 | ||
| 8178 | 9년 전 | 400 | ||
| 8177 |
kiplayer
|
9년 전 | 402 | |
| 8176 | 9년 전 | 430 | ||
| 8175 | 9년 전 | 315 | ||
| 8174 | 9년 전 | 314 | ||
| 8173 | 9년 전 | 404 | ||
| 8172 | 9년 전 | 281 | ||
| 8171 | 9년 전 | 243 | ||
| 8170 | 9년 전 | 366 | ||
| 8169 |
커네드커네드
|
9년 전 | 315 | |
| 8168 | 9년 전 | 399 | ||
| 8167 | 9년 전 | 387 | ||
| 8166 | 9년 전 | 292 | ||
| 8165 | 9년 전 | 237 | ||
| 8164 | 9년 전 | 368 | ||
| 8163 | 9년 전 | 362 | ||
| 8162 | 9년 전 | 365 | ||
| 8161 | 9년 전 | 380 | ||
| 8160 |
|
9년 전 | 578 | |
| 8159 | 9년 전 | 519 | ||
| 8158 | 9년 전 | 328 | ||
| 8157 | 9년 전 | 433 | ||
| 8156 | 9년 전 | 323 | ||
| 8155 | 9년 전 | 314 | ||
| 8154 |
00년생용띠
|
9년 전 | 656 | |
| 8153 | 9년 전 | 297 | ||
| 8152 |
|
9년 전 | 479 | |
| 8151 | 9년 전 | 477 | ||
| 8150 | 9년 전 | 581 | ||
| 8149 |
Jangfolk
|
9년 전 | 434 | |
| 8148 | 9년 전 | 248 | ||
| 8147 | 9년 전 | 446 | ||
| 8146 | 9년 전 | 528 | ||
| 8145 | 9년 전 | 476 | ||
| 8144 | 9년 전 | 438 | ||
| 8143 | 9년 전 | 273 | ||
| 8142 | 9년 전 | 489 | ||
| 8141 | 9년 전 | 437 | ||
| 8140 | 9년 전 | 992 | ||
| 8139 | 9년 전 | 344 | ||
| 8138 |
전갈자리남자
|
9년 전 | 450 | |
| 8137 | 9년 전 | 487 | ||
| 8136 | 9년 전 | 812 | ||
| 8135 |
|
9년 전 | 851 | |
| 8134 |
PlayPixel
|
9년 전 | 593 | |
| 8133 |
|
9년 전 | 505 | |
| 8132 | 9년 전 | 539 | ||
| 8131 | 9년 전 | 892 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기