위치 : 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 패턴이라고 하등가... 머 그럴겁니다.
게시판 목록
프로그램
| 번호 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|
| 730 |
|
19년 전 | 2728 | |
| 729 |
그레이스웹
|
19년 전 | 3520 | |
| 728 |
|
19년 전 | 2702 | |
| 727 |
|
19년 전 | 2539 | |
| 726 |
|
19년 전 | 2356 | |
| 725 |
|
19년 전 | 2260 | |
| 724 |
|
19년 전 | 2139 | |
| 723 |
|
19년 전 | 4264 | |
| 722 |
|
19년 전 | 2687 | |
| 721 |
|
19년 전 | 2178 | |
| 720 |
|
19년 전 | 2281 | |
| 719 |
|
19년 전 | 2858 | |
| 718 |
|
19년 전 | 1969 | |
| 717 |
|
19년 전 | 3198 | |
| 716 |
|
19년 전 | 2758 | |
| 715 |
|
19년 전 | 2282 | |
| 714 |
|
19년 전 | 1832 | |
| 713 |
|
19년 전 | 2249 | |
| 712 |
|
19년 전 | 2016 | |
| 711 |
|
19년 전 | 1913 | |
| 710 |
|
19년 전 | 2487 | |
| 709 |
|
19년 전 | 2340 | |
| 708 |
|
19년 전 | 3901 | |
| 707 |
|
19년 전 | 3744 | |
| 706 |
|
19년 전 | 2245 | |
| 705 |
|
19년 전 | 3321 | |
| 704 |
|
19년 전 | 1988 | |
| 703 |
|
19년 전 | 2392 | |
| 702 |
|
19년 전 | 2286 | |
| 701 |
홀로남은자
|
19년 전 | 2352 | |
| 700 |
홀로남은자
|
19년 전 | 2188 | |
| 699 | 19년 전 | 2820 | ||
| 698 |
|
19년 전 | 2846 | |
| 697 |
|
19년 전 | 3182 | |
| 696 |
|
19년 전 | 3052 | |
| 695 |
|
19년 전 | 2719 | |
| 694 |
|
19년 전 | 2953 | |
| 693 |
|
19년 전 | 3199 | |
| 692 |
|
19년 전 | 2885 | |
| 691 |
|
19년 전 | 2630 | |
| 690 | 19년 전 | 2934 | ||
| 689 | 19년 전 | 4518 | ||
| 688 | 19년 전 | 2442 | ||
| 687 | 19년 전 | 2456 | ||
| 686 | 19년 전 | 3490 | ||
| 685 | 19년 전 | 3177 | ||
| 684 | 19년 전 | 2840 | ||
| 683 | 19년 전 | 2068 | ||
| 682 | 19년 전 | 1780 | ||
| 681 | 19년 전 | 2880 | ||
| 680 | 19년 전 | 1734 | ||
| 679 | 19년 전 | 2314 | ||
| 678 | 19년 전 | 3966 | ||
| 677 | 19년 전 | 3745 | ||
| 676 | 19년 전 | 3474 | ||
| 675 | 19년 전 | 3414 | ||
| 674 |
|
19년 전 | 1571 | |
| 673 |
|
19년 전 | 1940 | |
| 672 |
|
19년 전 | 1868 | |
| 671 | 19년 전 | 2535 | ||
| 670 | 19년 전 | 4641 | ||
| 669 |
|
19년 전 | 2775 | |
| 668 |
|
19년 전 | 1931 | |
| 667 |
|
19년 전 | 1943 | |
| 666 |
|
19년 전 | 1854 | |
| 665 |
|
19년 전 | 2585 | |
| 664 |
|
19년 전 | 8185 | |
| 663 |
|
19년 전 | 2738 | |
| 662 |
|
19년 전 | 2798 | |
| 661 |
|
19년 전 | 3051 | |
| 660 |
|
19년 전 | 2224 | |
| 659 |
|
19년 전 | 2239 | |
| 658 |
|
19년 전 | 2183 | |
| 657 |
|
19년 전 | 2090 | |
| 656 |
|
19년 전 | 2279 | |
| 655 |
|
19년 전 | 2540 | |
| 654 |
|
19년 전 | 3085 | |
| 653 | 19년 전 | 2336 | ||
| 652 | 19년 전 | 1919 | ||
| 651 |
|
19년 전 | 2857 | |
| 650 | 19년 전 | 5021 | ||
| 649 | 19년 전 | 3512 | ||
| 648 | 19년 전 | 3468 | ||
| 647 | 19년 전 | 2980 | ||
| 646 | 19년 전 | 2414 | ||
| 645 | 19년 전 | 1504 | ||
| 644 | 19년 전 | 3146 | ||
| 643 | 19년 전 | 2026 | ||
| 642 |
|
19년 전 | 5401 | |
| 641 | 19년 전 | 2439 | ||
| 640 | 19년 전 | 3438 | ||
| 639 | 19년 전 | 2875 | ||
| 638 | 19년 전 | 1769 | ||
| 637 | 19년 전 | 3883 | ||
| 636 | 19년 전 | 2433 | ||
| 635 | 19년 전 | 2324 | ||
| 634 |
|
19년 전 | 3028 | |
| 633 |
|
19년 전 | 3308 | |
| 632 | 19년 전 | 2514 | ||
| 631 | 19년 전 | 2271 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기