위치 : 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 패턴이라고 하등가... 머 그럴겁니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 6830 |
112211dd
|
11년 전 | 787 | |
| 6829 |
진짜다진짜가나타남
|
11년 전 | 1190 | |
| 6828 | 11년 전 | 817 | ||
| 6827 |
봉보로봉봉
|
11년 전 | 900 | |
| 6826 |
jinion
|
11년 전 | 818 | |
| 6825 | 11년 전 | 800 | ||
| 6824 |
yunkiri486
|
11년 전 | 713 | |
| 6823 |
2donggalbi
|
11년 전 | 850 | |
| 6822 | 11년 전 | 844 | ||
| 6821 | 11년 전 | 797 | ||
| 6820 | 11년 전 | 3100 | ||
| 6819 | 11년 전 | 1248 | ||
| 6818 | 11년 전 | 516 | ||
| 6817 |
|
11년 전 | 602 | |
| 6816 | 11년 전 | 2070 | ||
| 6815 | 11년 전 | 1210 | ||
| 6814 | 11년 전 | 855 | ||
| 6813 | 11년 전 | 599 | ||
| 6812 |
|
11년 전 | 981 | |
| 6811 | 11년 전 | 576 | ||
| 6810 | 11년 전 | 1535 | ||
| 6809 |
낚시가좋아
|
11년 전 | 1047 | |
| 6808 | 11년 전 | 438 | ||
| 6807 | 11년 전 | 759 | ||
| 6806 |
Unhappy
|
11년 전 | 1770 | |
| 6805 | 11년 전 | 1594 | ||
| 6804 | 11년 전 | 1116 | ||
| 6803 | 11년 전 | 551 | ||
| 6802 |
asdfg
|
11년 전 | 1133 | |
| 6801 |
아트귀농인
|
11년 전 | 499 | |
| 6800 | 11년 전 | 2551 | ||
| 6799 | 11년 전 | 1352 | ||
| 6798 | 11년 전 | 1466 | ||
| 6797 | 11년 전 | 626 | ||
| 6796 |
purple63
|
11년 전 | 427 | |
| 6795 | 11년 전 | 2630 | ||
| 6794 |
커네드커네드
|
11년 전 | 955 | |
| 6793 | 11년 전 | 400 | ||
| 6792 | 11년 전 | 2489 | ||
| 6791 | 11년 전 | 510 | ||
| 6790 | 11년 전 | 2251 | ||
| 6789 |
리아빌리티
|
11년 전 | 3091 | |
| 6788 | 11년 전 | 1342 | ||
| 6787 | 11년 전 | 672 | ||
| 6786 | 11년 전 | 387 | ||
| 6785 | 11년 전 | 715 | ||
| 6784 |
|
11년 전 | 839 | |
| 6783 | 11년 전 | 577 | ||
| 6782 | 11년 전 | 3651 | ||
| 6781 |
리아빌리티
|
11년 전 | 478 | |
| 6780 | 11년 전 | 520 | ||
| 6779 | 11년 전 | 475 | ||
| 6778 | 11년 전 | 4677 | ||
| 6777 |
바보온달123
|
11년 전 | 610 | |
| 6776 | 11년 전 | 924 | ||
| 6775 | 11년 전 | 722 | ||
| 6774 |
DANet
|
11년 전 | 635 | |
| 6773 | 11년 전 | 1557 | ||
| 6772 | 11년 전 | 1350 | ||
| 6771 | 11년 전 | 610 | ||
| 6770 | 11년 전 | 1156 | ||
| 6769 | 11년 전 | 946 | ||
| 6768 | 11년 전 | 685 | ||
| 6767 | 11년 전 | 576 | ||
| 6766 | 11년 전 | 1259 | ||
| 6765 |
크림나이트
|
11년 전 | 991 | |
| 6764 | 11년 전 | 1532 | ||
| 6763 | 11년 전 | 2593 | ||
| 6762 | 11년 전 | 527 | ||
| 6761 |
|
11년 전 | 778 | |
| 6760 |
|
11년 전 | 706 | |
| 6759 | 11년 전 | 3341 | ||
| 6758 | 11년 전 | 1007 | ||
| 6757 | 11년 전 | 1265 | ||
| 6756 | 11년 전 | 887 | ||
| 6755 |
|
11년 전 | 567 | |
| 6754 |
|
11년 전 | 708 | |
| 6753 |
|
11년 전 | 1374 | |
| 6752 | 11년 전 | 598 | ||
| 6751 |
|
11년 전 | 647 | |
| 6750 |
|
11년 전 | 2013 | |
| 6749 | 11년 전 | 1207 | ||
| 6748 |
|
11년 전 | 1111 | |
| 6747 | 11년 전 | 1156 | ||
| 6746 | 11년 전 | 828 | ||
| 6745 |
|
11년 전 | 903 | |
| 6744 | 11년 전 | 846 | ||
| 6743 | 11년 전 | 1274 | ||
| 6742 | 11년 전 | 527 | ||
| 6741 |
Abilityarch
|
11년 전 | 600 | |
| 6740 | 11년 전 | 641 | ||
| 6739 |
leewin20
|
11년 전 | 1230 | |
| 6738 | 11년 전 | 500 | ||
| 6737 | 11년 전 | 1210 | ||
| 6736 | 11년 전 | 1250 | ||
| 6735 | 11년 전 | 545 | ||
| 6734 | 11년 전 | 1278 | ||
| 6733 |
RipperTNT
|
11년 전 | 1837 | |
| 6732 |
|
11년 전 | 1335 | |
| 6731 |
|
11년 전 | 1384 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기