위치 : 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년 전 | 798 | |
| 6829 |
진짜다진짜가나타남
|
11년 전 | 1220 | |
| 6828 | 11년 전 | 833 | ||
| 6827 |
봉보로봉봉
|
11년 전 | 920 | |
| 6826 |
jinion
|
11년 전 | 833 | |
| 6825 | 11년 전 | 810 | ||
| 6824 |
yunkiri486
|
11년 전 | 731 | |
| 6823 |
2donggalbi
|
11년 전 | 861 | |
| 6822 | 11년 전 | 862 | ||
| 6821 | 11년 전 | 810 | ||
| 6820 | 11년 전 | 3115 | ||
| 6819 | 11년 전 | 1263 | ||
| 6818 | 11년 전 | 522 | ||
| 6817 |
|
11년 전 | 620 | |
| 6816 | 11년 전 | 2089 | ||
| 6815 | 11년 전 | 1223 | ||
| 6814 | 11년 전 | 875 | ||
| 6813 | 11년 전 | 625 | ||
| 6812 |
|
11년 전 | 995 | |
| 6811 | 11년 전 | 595 | ||
| 6810 | 11년 전 | 1549 | ||
| 6809 |
낚시가좋아
|
11년 전 | 1053 | |
| 6808 | 11년 전 | 451 | ||
| 6807 | 11년 전 | 782 | ||
| 6806 |
Unhappy
|
11년 전 | 1788 | |
| 6805 | 11년 전 | 1623 | ||
| 6804 | 11년 전 | 1138 | ||
| 6803 | 11년 전 | 580 | ||
| 6802 |
asdfg
|
11년 전 | 1151 | |
| 6801 |
아트귀농인
|
11년 전 | 514 | |
| 6800 | 11년 전 | 2569 | ||
| 6799 | 11년 전 | 1364 | ||
| 6798 | 11년 전 | 1478 | ||
| 6797 | 11년 전 | 637 | ||
| 6796 |
purple63
|
11년 전 | 437 | |
| 6795 | 11년 전 | 2641 | ||
| 6794 |
커네드커네드
|
11년 전 | 967 | |
| 6793 | 11년 전 | 416 | ||
| 6792 | 11년 전 | 2510 | ||
| 6791 | 11년 전 | 535 | ||
| 6790 | 11년 전 | 2267 | ||
| 6789 |
리아빌리티
|
11년 전 | 3109 | |
| 6788 | 11년 전 | 1349 | ||
| 6787 | 11년 전 | 685 | ||
| 6786 | 11년 전 | 396 | ||
| 6785 | 11년 전 | 726 | ||
| 6784 |
|
11년 전 | 856 | |
| 6783 | 11년 전 | 596 | ||
| 6782 | 11년 전 | 3664 | ||
| 6781 |
리아빌리티
|
11년 전 | 503 | |
| 6780 | 11년 전 | 544 | ||
| 6779 | 11년 전 | 497 | ||
| 6778 | 11년 전 | 4705 | ||
| 6777 |
바보온달123
|
11년 전 | 625 | |
| 6776 | 11년 전 | 937 | ||
| 6775 | 11년 전 | 740 | ||
| 6774 |
DANet
|
11년 전 | 648 | |
| 6773 | 11년 전 | 1563 | ||
| 6772 | 11년 전 | 1368 | ||
| 6771 | 11년 전 | 625 | ||
| 6770 | 11년 전 | 1166 | ||
| 6769 | 11년 전 | 964 | ||
| 6768 | 11년 전 | 698 | ||
| 6767 | 11년 전 | 593 | ||
| 6766 | 11년 전 | 1274 | ||
| 6765 |
크림나이트
|
11년 전 | 1007 | |
| 6764 | 11년 전 | 1559 | ||
| 6763 | 11년 전 | 2617 | ||
| 6762 | 11년 전 | 547 | ||
| 6761 |
|
11년 전 | 797 | |
| 6760 |
|
11년 전 | 723 | |
| 6759 | 11년 전 | 3354 | ||
| 6758 | 11년 전 | 1015 | ||
| 6757 | 11년 전 | 1277 | ||
| 6756 | 11년 전 | 900 | ||
| 6755 |
|
11년 전 | 577 | |
| 6754 |
|
11년 전 | 727 | |
| 6753 |
|
11년 전 | 1396 | |
| 6752 | 11년 전 | 614 | ||
| 6751 |
|
11년 전 | 659 | |
| 6750 |
|
11년 전 | 2033 | |
| 6749 | 11년 전 | 1225 | ||
| 6748 |
|
11년 전 | 1128 | |
| 6747 | 11년 전 | 1179 | ||
| 6746 | 11년 전 | 844 | ||
| 6745 |
|
11년 전 | 919 | |
| 6744 | 11년 전 | 859 | ||
| 6743 | 11년 전 | 1299 | ||
| 6742 | 11년 전 | 543 | ||
| 6741 |
Abilityarch
|
11년 전 | 614 | |
| 6740 | 11년 전 | 664 | ||
| 6739 |
leewin20
|
11년 전 | 1244 | |
| 6738 | 11년 전 | 515 | ||
| 6737 | 11년 전 | 1221 | ||
| 6736 | 11년 전 | 1264 | ||
| 6735 | 11년 전 | 558 | ||
| 6734 | 11년 전 | 1293 | ||
| 6733 |
RipperTNT
|
11년 전 | 1847 | |
| 6732 |
|
11년 전 | 1343 | |
| 6731 |
|
11년 전 | 1401 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기