탈

저번달에는 php 의 class 에 미쳐서 찾았던 TIP


<?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개

이거 어디라 사용하실려구요? 그냥 매직함수 __call 사용한 것 뿐인데...
$this->addObject( new Log() );
$this->addObject( new Other() );

이부분.......상속말고 다른 Class 의 Method 을 자신의 Method 처럼 사용하는 부분입니다.
급할땐 용이하죠...
아~ ^^ 그건 그렇죠 ... new 새로 메모리에 공간을 개설하면 메모리 작으면 좀 버벅 거릴겁니다.
저 코드는 메모리에 차지하는 용량을 무시해도 될 정도의 코드예요.....ㅠ_ㅠ
워프는 콜리앗 ... class 로 만든 저 샘플은 개미 발에 미세먼지

혹 new 로 만드는게 버벅일꺼라 생각하시는거라면.......
Framework 쪽 한번 보시는것을 추천 드립니다.
Framework 쪽으로 하고는 있습니다. 사이트는 가벼워야하니까 ^^
제가 만들어 쓰고 있는 EventDispatcher 라는 클래스와 비슷하네요.
예를 들어 게시판에 로그인이 필요한 부분에는 Dispatcher 만 해주고 관련 처리는 외부 함수를 addEventHandler 메소드로 연결해 주기만 하면 되죠.
이런걸 Observer 패턴이라고 하등가... 머 그럴겁니다.
댓글을 작성하시려면 로그인이 필요합니다.

프로그램

8,187건
+
제목 글쓴이 날짜 조회
15-05-06 조회 3,649
15-04-30 조회 3,895
15-04-30 조회 5,635
15-04-28 조회 4,060
15-04-28 조회 3,378
15-04-16 조회 4,543
15-04-13 조회 4,287
15-04-12 조회 3,846
15-04-10 조회 3,757
15-04-09 조회 4,052
15-04-09 조회 3,642
15-04-07 조회 3,273
15-04-06 조회 4,038
15-04-04 조회 4,685
15-04-04 조회 4,197
15-04-03 조회 4,358
15-04-02 조회 4,865
15-04-01 조회 6,040
15-04-01 조회 3,541
15-03-30 조회 3,406
15-03-30 조회 4,046
15-03-28 조회 4,718
15-03-27 조회 3,725
15-03-26 조회 3,446
15-03-24 조회 3,535
15-03-23 조회 3,610
15-03-23 조회 3,918
15-03-20 조회 4,036
15-03-20 조회 4,807
15-03-18 조회 3,406
15-03-18 조회 3,617
15-03-02 조회 7,978
15-02-27 조회 3,407
15-02-27 조회 3,860
15-02-25 조회 3,929
15-02-23 조회 4,237
15-02-20 조회 3,911
15-02-19 조회 3,907
15-02-12 조회 4,203
15-02-11 조회 4,709
15-02-11 조회 4,438
15-02-11 조회 5,299
15-02-10 조회 4,685
15-02-09 조회 4,830
15-02-06 조회 3,806
15-02-04 조회 4,106
15-02-04 조회 3,887
15-02-04 조회 3,570
15-02-03 조회 3,420
15-02-02 조회 3,547
15-02-02 조회 4,482
15-01-31 조회 3,580
15-01-31 조회 4,667
15-01-30 조회 6,434
15-01-30 조회 4,012
15-01-30 조회 3,964
15-01-29 조회 3,888
15-01-29 조회 3,786
15-01-29 조회 3,455
15-01-29 조회 4,741
15-01-28 조회 3,352
15-01-28 조회 3,789
15-01-26 조회 4,063
15-01-26 조회 3,996
15-01-24 조회 5,496
15-01-23 조회 3,771
15-01-23 조회 4,309
15-01-22 조회 3,625
15-01-22 조회 3,591
15-01-22 조회 4,486
15-01-20 조회 3,898
15-01-19 조회 3,709
15-01-18 조회 4,478
15-01-17 조회 3,421
15-01-16 조회 4,184
15-01-16 조회 4,068
15-01-16 조회 7,318
15-01-16 조회 3,894
15-01-15 조회 3,448
15-01-13 조회 6,098
15-01-12 조회 4,059
15-01-09 조회 3,794
15-01-09 조회 3,301
15-01-09 조회 3,583
15-01-08 조회 4,502
15-01-08 조회 6,783
15-01-07 조회 5,040
15-01-06 조회 3,633
15-01-06 조회 3,183
15-01-05 조회 4,106
15-01-05 조회 4,214
15-01-04 조회 6,310
15-01-03 조회 3,888
14-12-31 조회 5,679
14-12-30 조회 4,890
14-12-30 조회 3,521
14-12-30 조회 3,982
14-12-30 조회 5,508
14-12-27 조회 4,055
14-12-27 조회 4,145