탈

저번달에는 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건
+
제목 글쓴이 날짜 조회
13-12-07 조회 3,123
13-12-06 조회 2,646
13-12-06 조회 3,192
13-12-06 조회 2,767
13-12-06 조회 2,980
13-12-06 조회 2,670
13-12-06 조회 3,405
13-12-06 조회 3,731
13-12-06 조회 2,755
13-12-06 조회 2,706
13-12-06 조회 3,796
13-12-05 조회 2,745
13-12-05 조회 3,339
13-12-05 조회 3,196
13-12-05 조회 3,605
13-12-05 조회 3,514
13-12-04 조회 8,328
13-12-03 조회 3,480
13-12-02 조회 2,767
13-12-02 조회 2,619
13-11-30 조회 2,554
13-11-29 조회 2,809
13-11-28 조회 3,021
13-11-27 조회 3,621
13-11-27 조회 3,011
13-11-27 조회 3,392
13-11-27 조회 2,543
13-11-27 조회 2,718
13-11-27 조회 4,761
13-11-26 조회 4,010
13-11-25 조회 3,504
13-11-25 조회 5,313
13-11-23 조회 2,690
13-11-23 조회 4,787
13-11-23 조회 4,343
13-11-23 조회 4,104
13-11-23 조회 4,046
13-11-22 조회 3,058
13-11-22 조회 3,735
13-11-22 조회 3,586
13-11-22 조회 3,135
13-11-22 조회 5,554
13-11-21 조회 2,787
13-11-21 조회 3,276
13-11-20 조회 3,645
13-11-19 조회 4,070
13-11-19 조회 3,080
13-11-19 조회 3,708
13-11-19 조회 3,057
13-11-19 조회 4,819
13-11-18 조회 3,094
13-11-18 조회 2,926
13-11-18 조회 3,241
13-11-18 조회 4,982
13-11-18 조회 4,380
13-11-18 조회 6,843
13-11-18 조회 4,071
13-11-18 조회 4,482
13-11-18 조회 3,853
13-11-18 조회 3,914
13-11-18 조회 3,177
13-11-16 조회 3,207
13-11-15 조회 4,438
13-11-15 조회 3,224
13-11-14 조회 2,973
13-11-14 조회 3,734
13-11-14 조회 4,091
13-11-13 조회 3,210
13-11-13 조회 3,254
13-11-13 조회 3,432
13-11-13 조회 4,310
13-11-13 조회 3,148
13-11-12 조회 3,472
13-11-12 조회 5,687
13-11-07 조회 3,207
13-11-07 조회 3,175
13-11-06 조회 3,118
13-11-05 조회 3,844
13-11-05 조회 3,616
13-11-05 조회 6,651
13-11-05 조회 3,594
13-11-04 조회 3,195
13-11-04 조회 3,212
13-11-04 조회 2,992
13-11-02 조회 3,656
13-11-02 조회 3,291
13-11-01 조회 3,548
13-11-01 조회 2,919
13-11-01 조회 3,064
13-10-31 조회 3,132
13-10-31 조회 3,521
13-10-31 조회 3,917
13-10-31 조회 3,110
13-10-30 조회 3,416
13-10-30 조회 7,688
13-10-30 조회 5,515
13-10-30 조회 3,387
13-10-29 조회 3,218
13-10-29 조회 4,384
13-10-29 조회 3,649