탈

저번달에는 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-03-24 조회 3,678
14-03-24 조회 3,471
14-03-23 조회 8,072
14-03-22 조회 3,368
14-03-21 조회 3,470
14-03-20 조회 5,981
14-03-19 조회 3,749
14-03-19 조회 7,470
14-03-19 조회 4,869
14-03-18 조회 5,597
14-03-18 조회 3,288
14-03-17 조회 3,378
14-03-17 조회 4,372
14-03-17 조회 3,502
14-03-17 조회 4,512
14-03-17 조회 4,487
14-03-17 조회 7,400
14-03-16 조회 3,878
14-03-16 조회 4,990
14-03-16 조회 4,234
14-03-15 조회 4,253
14-03-15 조회 4,371
14-03-15 조회 3,741
14-03-15 조회 4,474
14-03-15 조회 3,090
14-03-14 조회 6,774
14-03-14 조회 4,678
14-03-14 조회 4,488
14-03-13 조회 3,936
14-03-13 조회 9,500
14-03-13 조회 3,827
14-03-13 조회 3,546
14-03-13 조회 5,450
14-03-13 조회 4,396
14-03-13 조회 3,583
14-03-12 조회 4,100
14-03-12 조회 5,339
14-03-12 조회 4,690
14-03-12 조회 4,495
14-03-12 조회 4,924
14-03-12 조회 5,065
14-03-12 조회 6,019
14-03-12 조회 5,481
14-03-11 조회 5,731
14-03-11 조회 6,340
14-03-11 조회 6,131
14-03-11 조회 3,448
14-03-11 조회 6,929
14-03-11 조회 8,094
14-03-10 조회 3,635
14-03-08 조회 4,860
14-03-07 조회 3,973
14-03-07 조회 3,290
14-03-06 조회 3,759
14-03-06 조회 3,322
14-03-05 조회 5,469
14-03-04 조회 4,218
14-03-04 조회 3,294
14-03-04 조회 3,335
14-03-04 조회 3,789
14-03-03 조회 3,437
14-03-03 조회 3,564
14-03-02 조회 3,243
14-03-02 조회 3,293
14-02-28 조회 3,376
14-02-26 조회 3,252
14-02-26 조회 3,245
14-02-25 조회 3,404
14-02-25 조회 5,955
14-02-24 조회 3,681
14-02-24 조회 2,925
14-02-24 조회 2,966
14-02-24 조회 4,762
14-02-23 조회 3,896
14-02-20 조회 3,329
14-02-20 조회 2,985
14-02-20 조회 3,389
14-02-20 조회 2,988
14-02-18 조회 3,937
14-02-17 조회 3,388
14-02-17 조회 2,861
14-02-17 조회 3,068
14-02-17 조회 4,247
14-02-17 조회 3,890
14-02-17 조회 3,471
14-02-15 조회 2,841
14-02-14 조회 3,413
14-02-14 조회 2,810
14-02-14 조회 5,040
14-02-14 조회 3,741
14-02-14 조회 3,761
14-02-14 조회 3,002
14-02-14 조회 2,783
14-02-14 조회 5,110
14-02-14 조회 3,332
14-02-14 조회 2,819
14-02-14 조회 5,221
14-02-13 조회 2,992
14-02-13 조회 4,991
14-02-12 조회 5,703