탈

저번달에는 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건
+
제목 글쓴이 날짜 조회
14-10-25 조회 5,219
14-10-25 조회 6,409
14-10-25 조회 3,824
14-10-25 조회 3,973
14-10-24 조회 3,929
14-10-23 조회 4,545
14-10-22 조회 3,686
14-10-22 조회 4,363
14-10-22 조회 7,208
14-10-22 조회 4,914
14-10-22 조회 4,865
14-10-22 조회 9,028
14-10-19 조회 4,792
14-10-18 조회 5,758
14-10-17 조회 5,272
14-10-17 조회 4,053
14-10-17 조회 6,137
14-10-17 조회 5,529
14-10-17 조회 9,059
14-10-17 조회 6,537
14-10-17 조회 4,833
14-10-17 조회 5,154
14-10-14 조회 3,374
14-10-14 조회 4,289
14-10-13 조회 3,853
14-10-13 조회 6,533
14-10-13 조회 4,469
14-10-13 조회 4,364
14-10-13 조회 4,564
14-10-11 조회 6,732
14-10-11 조회 6,642
14-10-11 조회 6,524
14-10-10 조회 4,025
14-10-10 조회 6,483
14-10-10 조회 5,761
14-10-10 조회 6,336
14-10-10 조회 3,597
14-10-10 조회 5,511
14-10-10 조회 5,516
14-10-10 조회 5,589
14-10-10 조회 4,505
14-10-10 조회 4,998
14-10-10 조회 4,294
14-10-10 조회 4,141
14-10-08 조회 4,680
14-10-08 조회 3,789
14-10-08 조회 7,014
14-10-07 조회 6,222
14-10-07 조회 4,551
14-10-07 조회 5,575
14-10-07 조회 3,764
14-10-07 조회 4,747
14-10-07 조회 6,414
14-10-06 조회 6,769
14-10-06 조회 7,550
14-10-05 조회 3,756
14-10-04 조회 5,048
14-10-03 조회 6,358
14-10-02 조회 3,930
14-09-29 조회 4,121
14-09-29 조회 4,579
14-09-28 조회 3,904
14-09-27 조회 5,247
14-09-27 조회 4,705
14-09-26 조회 6,790
14-09-26 조회 5,224
14-09-25 조회 7,564
14-09-25 조회 4,074
14-09-25 조회 4,082
14-09-24 조회 4,878
14-09-24 조회 4,833
14-09-24 조회 4,330
14-09-24 조회 3,896
14-09-24 조회 4,516
14-09-24 조회 3,989
14-09-23 조회 4,165
14-09-23 조회 4,283
14-09-23 조회 4,065
14-09-22 조회 3,941
14-09-22 조회 4,865
14-09-22 조회 4,971
14-09-22 조회 6,188
14-09-22 조회 4,153
14-09-22 조회 3,841
14-09-22 조회 3,911
14-09-21 조회 5,846
14-09-21 조회 4,626
14-09-21 조회 4,457
14-09-21 조회 3,942
14-09-21 조회 4,499
14-09-21 조회 4,388
14-09-21 조회 4,069
14-09-20 조회 4,044
14-09-20 조회 3,949
14-09-20 조회 3,854
14-09-20 조회 4,581
14-09-20 조회 4,031
14-09-19 조회 3,942
14-09-19 조회 4,114
14-09-19 조회 4,323