탈

저번달에는 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,188건
+
제목 글쓴이 날짜 조회
16-06-11 조회 3,075
16-02-15 조회 3,305
16-02-02 조회 2,974
16-01-25 조회 2,588
16-01-18 조회 2,695
16-01-08 조회 2,607
16-01-07 조회 2,617
16-01-05 조회 2,653
16-01-05 조회 2,952
16-01-05 조회 2,926
16-01-05 조회 2,806
16-01-04 조회 2,667
16-01-04 조회 2,673
16-01-04 조회 2,622
16-01-04 조회 2,536
15-12-30 조회 2,887
15-12-24 조회 2,700
15-12-24 조회 2,697
15-12-24 조회 2,725
15-12-24 조회 2,678
15-12-23 조회 2,669
15-12-16 조회 2,761
15-12-15 조회 2,823
15-12-11 조회 2,948
15-12-10 조회 2,810
15-11-09 조회 2,792
15-11-09 조회 3,015
15-10-06 조회 3,351
15-10-02 조회 3,129
15-05-29 조회 4,462
15-02-02 조회 4,439
15-01-18 조회 4,425
14-10-25 조회 8,382
14-10-25 조회 5,162
14-10-22 조회 7,167
14-10-22 조회 4,867
14-10-22 조회 4,823
14-10-18 조회 5,688
14-10-17 조회 6,065
14-10-17 조회 5,447
14-10-17 조회 8,980
14-10-17 조회 6,459
14-10-13 조회 4,386
14-10-13 조회 4,487
14-10-07 조회 6,162
14-09-24 조회 3,932
14-09-23 조회 3,996
14-09-22 조회 4,903
14-09-22 조회 6,135
14-09-22 조회 4,100
14-09-22 조회 3,771
14-09-21 조회 4,393
14-09-21 조회 3,875
14-09-20 조회 3,879
14-09-20 조회 3,784
14-09-20 조회 3,961
14-09-19 조회 3,884
14-09-19 조회 4,048
14-09-19 조회 3,932
14-09-19 조회 4,313
14-09-19 조회 3,955
14-09-19 조회 3,884
14-09-19 조회 4,168
14-09-19 조회 3,901
14-09-19 조회 3,948
14-09-19 조회 3,937
14-09-18 조회 4,014
14-09-18 조회 3,969
14-09-18 조회 4,238
14-09-18 조회 4,203
14-09-18 조회 4,217
14-09-18 조회 4,915
14-09-18 조회 5,469
14-09-18 조회 5,358
14-09-18 조회 4,161
14-09-18 조회 4,652
14-09-18 조회 4,270
14-08-30 조회 5,538
14-08-06 조회 6,023
14-08-06 조회 5,638
14-08-03 조회 5,275
14-06-12 조회 5,545
14-05-11 조회 3,731
14-05-11 조회 4,492
14-05-11 조회 4,595
14-05-09 조회 1.3만
14-05-01 조회 5,193
14-03-30 조회 4,397
14-03-23 조회 8,019
14-02-12 조회 3,901
13-10-29 조회 7,862
13-10-28 조회 8,690
13-09-29 조회 6,656
13-09-29 조회 4,285
13-07-23 조회 5,158
13-07-23 조회 1.2만
13-07-14 조회 4,690
13-07-14 조회 5,079
13-07-14 조회 6,042
13-07-14 조회 5,320